I've noticed some CMS sites doing this so I figured it might be useful to do on my own site. Basically the gist of this function is to create a CSS class name from the URL. This class can then be referenced in your stylesheet and you can be assured that it will only target that specific page. This is most useful especially with specificity problems.

  1. Remove First String Character In ASP Classic

    Input String = "/products/computers/parts/default.asp"

    First lets lower case that url

    url = lcase(request.ServerVariables("URL")) url = right(url,len(url)-1)

    What this does is moves the length of the string minus 1 space from the right position of the string using the right() and len() built in functions in ASP. I realize that there are probably easier ways to do this in other languages but I figured I'd fill that empty hole on the internet in this subject by writing it up.

  2. Remove Forward Slashes

    Class Name = "products/computers/parts/default.asp"

    Next lets remove all forward slashes and replace them with underscores. (Note you can use whatever separator your want, I prefer underscores for readability.

    url = replace(url,"/","_")
  3. Remove File Extension And Add Prefix

    Class Name = "products_computers_parts_default.asp"

    Last, lets remove the file extension and give the class a prefix of "url_" to set it apart from other class names.

    url = replace(url,".asp","") CreatePageClassName = "url_" & url

Putting It All Together

function CreatePageClassName() url = lcase(request.ServerVariables("URL")) url = right(url,len(url)-1) url = replace(url,"/","_") url = replace(url,".asp","") CreatePageClassName = "url_" & url end function

Class Name = "url_products_computers_parts_default"

Where To Use This

I would personally place this on your body tag as an ID if possible:

<body id="<%=CreatePageClassName()%>">

Which would result in this:

<body id="url_products_computers_parts_default">