Creating Dynamic Style Sheets Using ASP

Browser Detection

It's pretty easy in fact, to combine this technique with browser detection, producing a style sheet that will work in any browser.

'Detect Client Browser Environment
Dim strUserAgent ' browser type capture
Dim IE5Plus, IE55Plus, css2compatible ' general purpose capabilities
Dim NS, NS4, NS6, IE, IE4, IE5, IE6, Opera, Opera5 ' Browser Boolean Constants
Dim OtherBrowser ' obsolete browser flags
 
strUserAgent = LCase(cstr(request.ServerVariables("HTTP_USER_AGENT")))
 
'Explorer
IE = FALSE
IE4 = FALSE
IE5 = FALSE
IE6 = FALSE
If InStr(strUserAgent, "MSIE") Then
    IE = TRUE
End If
If InStr(strUserAgent, "MSIE 4") Then
    IE4 = TRUE
ElseIf InStr(strUserAgent, "MSIE 5") Then
    IE5 = TRUE
ElseIf InStr(strUserAgent, "MSIE 6") Then
    IE6 = TRUE
End If
 
'Opera
Opera = FALSE
Opera5 = FALSE
If InStr(strUserAgent, "Opera") Then
    Opera = TRUE
End If
If InStr(strUserAgent, "Opera 5") _
Or InStr(strUserAgent, "Opera/5") Then
    Opera5 = TRUE
End If
 
'Netscape
NS = FALSE
NS4 = FALSE
NS6 = FALSE
If InStr(strUserAgent, "Netscape6") Then
    NS6 = TRUE
ElseIf InStr(strUserAgent, "Mozilla/4") AND Not (IE OR Opera) Then
    NS4 = TRUE
End If
'This may not be 100% accurate; there are a lot of browsers based on the Mozilla core
If NS6 OR NS4 OR (InStr(strUserAgent, "Mozilla") AND Not (IE OR Opera)) Then
    NS = TRUE
End If
 
'If the document looses the Loose DTD instead of Transitional or Strict
'then set css2compatible to FALSE for IE6 instead
If Opera5 Or IE6 Or NS6 Then
    css2compatible = TRUE
Else
    css2compatible = FALSE
End If
 
OtherBrowser = FALSE
If Not (IE OR NS4 OR NS6 OR Opera) Then
    OtherBrowser = TRUE
End If
If InStr(strUserAgent, "MSIE 5") _
Or InStr(strUserAgent, "MSIE 6") Then
    IE5Plus = TRUE
Else
    IE5Plus = FALSE
End If

You might also like...

Comments

About the author

Thomas C. Carpe United States

I have been working in IT since 1993. I founded CarpeDiem Business Internet Systems in 1995. In 2000 we incroporated and took on two partners. Its really a grat lot of fun, and I enjoy working o...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan