PDA

View Full Version : Browser Detection Fun


llbbl
05-20-2005, 10:58 AM
What is the best way to do browser detection? Post some code that you find and links to it.

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
browser = "Konqueror";
OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
browser = "Netscape Navigator"
version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
if (checkIt('linux')) OS = "Linux";
else if (checkIt('x11')) OS = "Unix";
else if (checkIt('mac')) OS = "Mac"
else if (checkIt('win')) OS = "Windows"
else OS = "an unknown operating system";
}

function checkIt(string)
{
place = detect.indexOf(string) + 1;
thestring = string;
return place;
}

http://www.quirksmode.org/js/detect.html#link10

Is there any better than that? I have seen lots of ways to do it. He says in the article that you shouldn't use navigator.appName because it is unreliable.

llbbl
05-20-2005, 11:07 AM
see this guy used appname.


<SCRIPT LANGUAGE = "JavaScript"><!--
// Script by Iceman, dciceman@usa.net. You can use this script providing
//this header stays in. Copyright 1997 Iceman.
//the parent.location.href statements can be changed to get different redirections for different browsers.

var r
browser = navigator.appName

version="4.0 (compatible; MSIE 4.0b1; Windows 95)"
if (browser == "Microsoft Internet Explorer" && navigator.appVersion==version) {
//If Browser is MSIE 4.0b1 then go to MSIE Page.
parent.location.href="msiepage.html"
r=2
}

else {
ua="Mozilla/2.0 (compatible; MSIE 3.02; Windows 95)"
if (navigator.userAgent==ua){
//If Browser is MSIE 3.02 go to MSIE Page.
parent.location.href="msiepage.html"
r=2
}

version="4.0 (compatible; MSIE 4.0b2; Windows 95)"
if (browser == "Microsoft Internet Explorer" && navigator.appVersion==version) {
//If Browser is MSIE 4.0b2 then go to MSIE Page.
parent.location.href="msiepage.html"
r=2
}

version="2.01 (Win95; I)"
if (navigator.appVersion==version && navigator.appCodeName=="Mozilla") {
//If Browser is Netscape 2.01 go to Netscape page.
parent.location.href="nspage.html"
r=1
}

version="2.0 (Win95; I; 16bit)"
if (navigator.appVersion==version && navigator.appCodeName=="Mozilla") {
//If Browser is Netscape 2.0 16 bit go to Netscape page.
parent.location.href="nspage.html"
r=1
}

version="4.01 [en] (Win95; I)"
if (navigator.appVersion==version && navigator.appCodeName=="Mozilla") {
//If Browser is Netscape 4.01 go to Netscape page.
parent.location.href="nspage.html"
r=1
}

version="3.0 (Win95; I)"
if (browser == "Netscape" && navigator.appVersion==version) {
// If browser is Netscape 3.0 for Win95 go to Netscape Page.
parent.location.href="nspage.html"
r=1
}
}

if (browser=="Netscape") {
// If browser is Netscape and Supports JavaScript go to Netscape Page
parent.location.href="nspage.html"
r=1
}

if (r !=1 && r !=2){
// if browser is not Nestcape or MSIE known versions go to MSIE page.
//(caters for any other JavaScript Browsers).
parent.location.href="msiepage.html"
}

// -->
</SCRIPT>


http://www.htmlgoodies.com/legacy/beyond/javascript/browserpage.txt