Skip to main content
April 13, 2009
Answered

How to detect the .cfm currently being visited.

  • April 13, 2009
  • 2 replies
  • 840 views

When the user is at "index.cfm", how can I write a cfif tag to detect this?

Basically I want my navigation menu to look different depending on the user's location ofcourse I could simply change the CSS of the menu for each page, but I'm using cfinclude to place the menu around the site, so there's only 1 version of the menu.

Thanks!

    This topic has been closed for replies.
    Correct answer craigkaminsky

    Without knowing the larger picture of your web application, here are a few places/ways to determine the current page:

    If you are using Application.cfc, the current page is passed as a parameter to the onRequestStart method. The parameter is called 'targetPage' by default (though I imagine you could change that) and it contains the path, from the web root, to the page that's being called. If the page being called is www.mydomain.com/index.cfm, this parameter's value would be 'index.cfm'.

    Example:

    <cffunction name="onRequestStart">

      <cfargument name="targetPage" />

      <cfif targetPage is "index.cfm">

        <!--- do something here based on page being requested --->

      </cfif>

    </cffunction>

    You can also use CGI variables on any CML page in your application to get the page name. Of the many variables in the CGI scope, script_name is probably the best one to use.

    Example:

    <cfif cgi.script_name is "index.cfm">

      <!--- do something here based on page being requested --->

    </cfif>

    Hopefully, the above will get you started. Good luck and have fun!

    2 replies

    craigkaminskyCorrect answer
    Inspiring
    April 13, 2009

    Without knowing the larger picture of your web application, here are a few places/ways to determine the current page:

    If you are using Application.cfc, the current page is passed as a parameter to the onRequestStart method. The parameter is called 'targetPage' by default (though I imagine you could change that) and it contains the path, from the web root, to the page that's being called. If the page being called is www.mydomain.com/index.cfm, this parameter's value would be 'index.cfm'.

    Example:

    <cffunction name="onRequestStart">

      <cfargument name="targetPage" />

      <cfif targetPage is "index.cfm">

        <!--- do something here based on page being requested --->

      </cfif>

    </cffunction>

    You can also use CGI variables on any CML page in your application to get the page name. Of the many variables in the CGI scope, script_name is probably the best one to use.

    Example:

    <cfif cgi.script_name is "index.cfm">

      <!--- do something here based on page being requested --->

    </cfif>

    Hopefully, the above will get you started. Good luck and have fun!

    April 13, 2009

    Awesome

    I used "cgi.script_name" in the end... although the result given back was actually more like "/index.cfm".

    Thanks a lot!

    tclaremont
    Inspiring
    April 13, 2009

    If you want to remove the slash, just use REREPLACE on the variabe to replace the slashes with whatever you want.

    As long as you are going down this path, note that the following CGI variable will give you the page that the person came from:

    #CGI.HTTP_REFERER#

    This is sometimes useful if you have a page that you use a lot, with modifications depending on where the person came from.

    davidsimms
    Inspiring
    April 13, 2009

    #CGI.SCRIPT_NAME#