Welcome to my ASP Code Website

Browser-Specific Code for IE and Netscape



If you are designing a website, it is critical that you understand the differences between IE - Internet Explorer - and Netscape, and code for both.

The first thing you need to do is figure out what type of browser your visitor is using. You would do that with this code:

Dim SvrVar
Set SvrVar = Request.ServerVariables
BrowserName = SvrVar("HTTP_USER_AGENT")
OpSys = SvrVar("HTTP_UA_OS")
Set SvrVar = Nothing

Now you know exactly what browser and what operating system your user is using. Next, you would set up a separate style sheet for each browser. Different browsers use different stylesheet commands - and different operating systems use different fonts. Here would be a way to handle the four main combinations:

'SHOW STYLESHEET FOR MATCHING SYSTEM'
if InStr(BrowserName, "MSIE") > 0 and InStr(BrowserName, "Win") > 0 then
Response.Write "<link rel='stylesheet' href='/_css/windows/ie.css'>"
end if
if InStr(BrowserName, "MSIE") > 0 and InStr(BrowserName, "Win") = 0 then
Response.Write "<link rel='stylesheet' href='/_css/mac/ie.css'>"
end if
if InStr(BrowserName, "MSIE") = 0 and InStr(BrowserName, "Win") > 0 then
Response.Write "<link rel='stylesheet' href='/_css/windows/netscape.css'>"
end if
if InStr(BrowserName, "MSIE") = 0 and InStr(BrowserName, "Win") = 0 then
Response.Write "<link rel='stylesheet' href='/_css/windows/netscape.css'>"
end if

That takes care of the basics. Let's now get on to the actual page construction. One big difference between IE and Netscape is that IE uses body parameters of "topmargin" and "leftmargin", while Netscape goes with "marginheight" and "marginwidth". So your code to write your body statement could look like this:

if InStr(BrowserName, "MSIE") > 0 then
Response.Write "<body bgcolor='white' text='#333366' link='#ff6666' vlink='#ff6600' topmargin='0' leftmargin='0'>"
else
Response.Write "<body bgcolor='white' text='#333366' link='#ff6666' vlink='#ff6600' marginheight='0' marginwidth='0'>"
end if

You would use this same logic all through your code, making sure to write it appropriately based on what browser and operating system your user was working with. Yes, much of HTML is understood by all browsers, but you want your website to look at perfect as possible in all conditions. It is your responsibility as a developer to test your code on IE and Netscape, on PCs and Macs, to understand what the world sees when they view your website!

ASP Utility Code