Skip to main content
New Participant
July 13, 2010
Answered

Use CGI Variables to Assign body ID

  • July 13, 2010
  • 1 reply
  • 983 views

I need help with a dynamic way to assign an ID to the body tag on static CFM pages.

I've been doing this manually, and was hoping to be able to do it with the cgi.SCRIPT_NAME variable.  The problem is the ".cfm" gets included in the variable name.  Also, if the page is in a sub-directory, the path also gets included.

I'd like to be able to dynamically at the page name, truncating the extension and path.  Is this possible in CF8/CF9?  Any help would be greatly appreciated.

    This topic has been closed for replies.
    Correct answer ilssac

    That's exactly what I was looking for.

    If I understand this snippet correctly....

    • It looks for the variable cgi.script_name
    • then truncates every thing preceeding and including the "/" and the "\"  -- I guess this accounts for the directory structure and the "http://"
    • then trundates everything after and including the "."
    • and finally outputs the resulting page name

    Is this right?


    To stick with the function names I would say it would be more correct to describe it this way.

    1. The listLast() function looks at the cgi.script_name string as a list of elements separated by either forward or backward slash characters (\/) and returns the last item in the list.
    2. The listFirst() function loosk at the string returned by the listLast() function as a list of elements separated by the dot|period character (.) and returns the first item in the list.
    3. It then outputs this string, which happens to be a file|page name.

    1 reply

    ilssac
    Inspiring
    July 13, 2010

    And CF 7, 6, 5, and 4.5.  Earlier then that is before my time.

    Take a look at the list manipulation functions.

    What you want to do can be very easily done with a combonation of the listLast() and ListFirst() functions.

    You may want to also want to look at the system functions, there maybe one there that would work better for you then the cgi.script_name.  Though there is really nothing wrong with cgi-script_name.

    New Participant
    July 13, 2010

    Thanks for your prompt reply, Ian.  And thank you for the introduction to list functions and system functions -- I spend about 90% of my time in HTML/CSS, so this is new to me - so new that I'm not even certain what to do with them.  I guess a better question would have been how to truncate the ".cfm" and the preceeding path name from the SCRIPT_NAME variable.

    Are you suggesting that I should use the listLast() and listFirst() functions for the purpose of truncation, or is there something else I'm missing?

    New Participant
    July 13, 2010

    To stick with the function names I would say it would be more correct to describe it this way.

    1. The listLast() function looks at the cgi.script_name string as a list of elements separated by either forward or backward slash characters (\/) and returns the last item in the list.
    2. The listFirst() function loosk at the string returned by the listLast() function as a list of elements separated by the dot|period character (.) and returns the first item in the list.
    3. It then outputs this string, which happens to be a file|page name.

    You've been a big help.  Thank you very much!