Link to home
Start Free TrialLog in
Avatar of lhutton
lhutton

asked on

Simple Browser Detect

In IE 5.x it thinks it's IE 4.x. What am I doing wrong?

bName = navigator.appName;
bVer = parseInt(navigator.appVersion);
if(bName == "Netscape" && bVer == 3) ver = "n3";
else if (bName == "Netscape" && bVer == 4) ver = "n4";
else if (bName == "Microsoft Internet Explorer" && bVer == 3) ver = "e3";
else if (bName == "Microsoft Internet Explorer" && bVer == 4) ver = "e4";
else if (bName == "Microsoft Internet Explorer" && bVer == 5) ver = "e5";
if (ver == "n3") {
  document.write("Your Browser: Netscape Navigator 3.x");
}
if (ver == "n4") {
  document.write("Your Browser: Netscape Navigator 4.x");
}
if (ver == "e3") {
  document.write("Your Browser: Microsoft Internet Explorer 3.x");
}
if (ver == "e4") {
  document.write("Your Browser: Microsoft Internet Explorer 4.x");
}
if (ver == "e5") {
  document.write("Your Browser: Microsoft Internet Explorer 5.x");
}
Avatar of ssathya
ssathya
Flag of United States of America image

Because,
navigator.appVersion returns something like this,
4.0 (compatible; MSIE 5.0; Windows NT 5.0)
in IE 5.x,
So,
parseInt(navigator.appVersion) is always going to return 4...
 :-)

Hope it helps
Avatar of lhutton
lhutton

ASKER

How can I identify IE 5 and display a message saying IE 5 is being used?
This will work fine:

<html>
<script>
bName = navigator.appName;
bVer = parseInt(navigator.appVersion);
if(bName == "Netscape" && bVer == 3) ver = "n3";
else if (bName == "Netscape" && bVer == 4) ver = "n4";
else if (bName == "Microsoft Internet Explorer" && bVer == 3) ver = "e3";
else if (bName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf('5', 0) != -1) ver = "e5";
else if (bName == "Microsoft Internet Explorer" && bVer == 4) ver = "e4";
if (ver == "n3") {
  document.write("Your Browser: Netscape Navigator 3.x");
}
if (ver == "n4") {
  document.write("Your Browser: Netscape Navigator 4.x");
}
if (ver == "e3") {
  document.write("Your Browser: Microsoft Internet Explorer 3.x");
}
if (ver == "e4") {
  document.write("Your Browser: Microsoft Internet Explorer 4.x");
}
if (ver == "e5") {
  document.write("Your Browser: Microsoft Internet Explorer 5.x");
}
</script>
</html>

(notice that it detects IE5 before IE4, this is due to the else if)You could also use this, slightly simpler code:

<html>
<script>
bName = navigator.appName;
bVer = parseInt(navigator.appVersion);
if (bName == "Netscape" && bVer == 3) ver = "n3";
if (bName == "Netscape" && bVer == 4) ver = "n4";
if (bName == "Microsoft Internet Explorer" && bVer == 3) ver = "e3";
if (bName == "Microsoft Internet Explorer" && bVer == 4) ver = "e4";
if (bName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf('5', 0) != -1) ver = "e5";
if (ver == "n3") {
  document.write("Your Browser: Netscape Navigator 3.x");
}
if (ver == "n4") {
  document.write("Your Browser: Netscape Navigator 4.x");
}
if (ver == "e3") {
  document.write("Your Browser: Microsoft Internet Explorer 3.x");
}
if (ver == "e4") {
  document.write("Your Browser: Microsoft Internet Explorer 4.x");
}
if (ver == "e5") {
  document.write("Your Browser: Microsoft Internet Explorer 5.x");
}
</script>
</html>

(notice the lack of 'else' and the placement of the IE5 detection.

Regards,
Mårten
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use parseInt instead of parseFloat if you do not want the minor version

Michel
*smile* You are very true mplunjan, can't compete with that :/

Ha en rolig =) kväll,
Mårten
Avatar of lhutton

ASKER

Thanks to you both!
Avatar of lhutton

ASKER

Michel, if you have a moment, could you please take a look at my question at https://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=11513958  - I want to set a cookie that will remember a user's selection of a background colour across the site at http://www.geocities.com/hutton_l/customise.html

I already have the form set up and already have the basic cookie set up at http://www.geocities.com/hutton_l/form.js

Thanks