iEntry 10th Anniversary Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register

Go Back   WebForumz.com > Resources > Tutorials > ASP
Register All Albums Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read

Author


robbied72
I have worked in both Public and Private sectors over the last 12 years in the Information Technology industry, I am the Web Manager for a Local Authority but I still am pretty much hands on with coding in ASP, XHTML, CSS, JavaScript, XML and much more.

RSS Feed

Your Options
CSS selection based on Browser and OS

This ASP solution allows different CSS files to be included depending on browser and OS.
I had a problem not so long ago that i needed to build a solution to render pages correctly depending on browser and OS, here is the code from my work around.

browser_check.asp

  <%
'------------------Robbie Done 2008--------------------
'------------------www.bizmo.co.uk---------------------
'Get Browser
if instr(request.serverVariables("HTTP_USER_AGENT"),"MSIE")> 0 Then
Session("svBrowser") = "ie"
Else if instr(request.serverVariables("HTTP_USER_AGENT"),"Firefox")> 0 Then
Session("svBrowser") = "firefox"
Else if instr(request.serverVariables("HTTP_USER_AGENT"),"Safari")> 0 Then
Session("svBrowser") = "safari"
Else if instr(request.serverVariables("HTTP_USER_AGENT"),"Opera")> 0 Then
Session("svBrowser") = "opera"
End If
End If
End If
End If

'Get OS
if instr(request.serverVariables("HTTP_USER_AGENT"),"Macintosh")> 0 Then
Session("svOS") = "osx"
Else if instr(request.serverVariables("HTTP_USER_AGENT"),"Windows")> 0 Then
Session("svOS") = "windows"
End If
End If
%>
<%
'Name Style Sheet
Dim vBrowser,vOS,vFileName

vBrowser = Session("svBrowser")
vOS = Session("svOS")
vFileName = vBrowser&"_"&vOS&".css"
If vFileName = "firefox_windows.css" Then
vFileName = "style.css"
End If
%>
<!-- Show default style sheet -->
<link href="style.css" rel="stylesheet" type="text/css" media="screen">
<!-- Pick up amended browser/Os specific style sheet
depending on browser and OS-->
<%If vFileName <> "firefox_windows.css" AND vFilename <> "style.css" Then%>
<!-- I always style for FireFox and Windows first and work on others secondly,
so the statment above is basically saying that if the dynamic name is
'firefox_windows.css' then don't put a secondary sheet in but use style.css,
but if it is a different browser/os then add the secondary sheet in -->
<link href="<%=vFileName%>" rel="stylesheet" type="text/css" media="screen">
<%End If%>
Put this code in the head of your page and create the style combinations :-
  • ie_windows.css
  • firefox_osx.css
  • opera_windows.css
  • safari_windows.css
  • safari_osx.css
There may be more file's than the list above but it is a good start.

One point is that some browsers will render the same or there abouts as Firefox, so you will not necessarily need to create a separate sheet as style.css will be fine for rendering.

You may also want to separate the operations into 2 files if you are going to use this on multiple pages.

inc_browser_check.asp
<%
'------------------Robbie Done 2008--------------------
'------------------www.bizmo.co.uk---------------------
'Get Browser
if instr(request.serverVariables("HTTP_USER_AGENT"),"MSIE")> 0 Then
Session("svBrowser") = "ie"
Else if instr(request.serverVariables("HTTP_USER_AGENT"),"Firefox")> 0 Then
Session("svBrowser") = "firefox"
Else if instr(request.serverVariables("HTTP_USER_AGENT"),"Safari")> 0 Then
Session("svBrowser") = "safari"
Else if instr(request.serverVariables("HTTP_USER_AGENT"),"Opera")> 0 Then
Session("svBrowser") = "opera"
End If
End If
End If
End If

'Get OS
if instr(request.serverVariables("HTTP_USER_AGENT"),"Macintosh")> 0 Then
Session("svOS") = "osx"
Else if instr(request.serverVariables("HTTP_USER_AGENT"),"Windows")> 0 Then
Session("svOS") = "windows"
End If
End If
%>
a_dynamic_page.asp
Place in the <head></head> tags...
 <%
'Name Style Sheet
Dim vBrowser,vOS,vFileName

vBrowser = Session("svBrowser")
vOS = Session("svOS")
vFileName = vBrowser&"_"&vOS&".css"
If vFileName = "firefox_windows.css" Then
vFileName = "style.css"
End If
%>
<!-- Show default style sheet -->
<link href="style.css" rel="stylesheet" type="text/css" media="screen">
<!-- Pick up amended browser/Os specific style sheet depending on browser and OS-->
<%If vFileName <> "firefox_windows.css" AND vFilename <> "style.css" Then%>
<!-- I always style for FireFox and Windows first and work on others secondly, so
the statment above is basically saying that if the dynamic name
is 'firefox_windows.css' then don't
put a secondary sheet in but use style.css, but if it is a different
browser/os then add the secondary sheet in -->
<link href="<%=vFileName%>" rel="stylesheet" type="text/css" media="screen">
<%End If%>
Hope you guys and girls can use this.....

Attached Files


Members's Comments
Posted at June 19th, 2008 by rogerrkh
I am completely new to asp. Could someone point me to a good tutorial please
Posted at June 18th, 2008 by Rob
I was always of the opinion, and belief that good html and css coding should not warrant different css for different browsers but I'll be the first to admit that the situation isnt always that in reality....

Nice addition, thanks.
Page 1 of 1


Search Engine Optimization by vBSEO 3.2.0 RC8