Jump to content

Browser detection (oh no not again)


Deleriad
 Share

Recommended Posts

I'm sure this is an old, old issue. Basically, I want to be able to load a style sheet appropriate to the browser. The particular issue is that IE7 and Firefox use the w3c box model while IE6 and below use their own model. As my pages make extensive use of absolutely positioned divs then this is very important.I'm trying to keep it simple and as far as I know, this code should work

<script language="JavaScript" type="text/javascript"> 	 <!--     browserVersion = parseInt(navigator.appVersion) 	 if (navigator.appName == "Microsoft Internet Explorer" && browserVersion <= 6) {   	 document.writeln("<link rel='stylesheet' type='text/css' href='css/EHE-IE6.css' />"); 	 } 	 else  {     	 document.writeln("<link rel='stylesheet' type='text/css' href='css/EHE.css' />"); 	 } 	 //-->  </script>

It works fine for Firefox but when I try IE7 it detects it as though it's IE6. I only know javascript well enough to copy and paste so it could be something really, really stunned that I am doing wrong.

Link to comment
Share on other sites

Well I have discovered that for whatever reason, IE has always identified itself as Mozilla. IE7 identifies itself as mozilla version 4, the same as IE6. The w3schools detection program returns the following about IE7

CodeName=MozillaMinorVersion= Beta 2Name=Microsoft Internet ExplorerVersion=4.0 (compatible; MSIE 7.0; Windows NT 5.1; FunWebProducts; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)...UA=Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; FunWebProducts; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)

Somehow then I need to be able to test for MSIE 7.0 as part of the detection. Any ideas about how to deal with this?For the record, Firefox reads

CodeName=MozillaMinorVersion=undefinedName=NetscapeVersion=5.0 (Windows; en-GB)UA=Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8) Gecko/20051107 Firefox/1.5

Basically, the more I look at this the more horrible it looks. Opera, for example, identifies itself as Internet Explorer...

Link to comment
Share on other sites

Try this, i don't have IE7 to test it with but here's hoping :) For a start it correctly identifies each browser and from what i can gather the version of IE...

<head><script>function browser(){var agt=navigator.userAgent.toLowerCase();if (agt.indexOf("opera") != -1) return 'using Opera';;if (agt.indexOf("firefox") != -1) return 'using Firefox';if (agt.indexOf("msie") != -1) {       var version=0;       temp=navigator.appVersion.split("MSIE")    version=parseFloat(temp[1])       if (version<7)     return 'using IE6 or lower';    else     return 'using IE7';  }}</script></head><body><script>var browser=browser();alert(browser);</script></body>

Link to comment
Share on other sites

if(navigator.userAgent.match(/MSIE (\d).(\d+)/i) != null){  if(RegExp.$1 == "7"){     //IE 7  }}

You can check RegExp.$1 for version number, and RegExp.$2 for the .5's etc.Eep, didn't see Scott's above before posting. His is a lot better :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...