Click to See Complete Forum and Search --> : Browser and operating system script...


bjoerndalen
07-28-2003, 04:15 PM
Hi guys...

I've written a script which I was hoping someone could check for me..

I've set it up to check for the browser type and version as well as operating system.It works ok...


I would like someone to explain why it works when the browser is AOL and when Windows XP is used. I didnt think I'd set it up for these parameters.

<html>
<head>

<script language = "JavaScript">

<!--

function getBrowserName()
{
var lsBrowser = navigator.appName;
if (lsBrowser.indexOf("Microsoft") >= 0)
{
lsBrowser = "MSIE";
}
else if (lsBrowser.indexOf("Netscape") >= 0)
{
lsBrowser = "NETSCAPE";
}
else
{
lsBrowser = "UNKNOWN";
}
return lsBrowser;

}


function getOS()
{
var userPlat = "unknown";
var navInfo = navigator.userAgent;

if ((navInfo.indexOf("windows NT") != -1)
|| (navInfo.indexOf("windows 95") != -1)
|| (navInfo.indexOf("windows 98") != -1)
|| (navInfo.indexOf("WinNT") != -1)
|| (navInfo.indexOf("Win95") != -1)
|| (navInfo.indexOf("Win98") != -1))
{
userPlat = "Win32";
}
else if(navInfo.indexOf("Win16") != -1)
{
userPlat = "Win16";
}
else if(navInfo.indexOf("Macintosh") != -1)
{
userPlat = "PPC";
}
else if(navInfo.indexOf("68K") != -1)
{
userPlat = "68K";
}
return userPlat;
}


function getBrowserVersion()
{
var findIndex;
var browserVersion = 0;
var browser = getBrowserName();

if (browser == "MSIE")
{
browserVersion = navigator.userAgent;
findIndex = browserVersion.indexOf(browser) + 5;
browserVersion = parseInt(browserVersion.substring(findIndex,findIndex + 1));
}
else
{
browserVersion = parseInt(navigator.appVersion.substring(0,1));
}
return browserVersion;
}

//-->
</script>

</head>

<body>
<script language = "JavaScript">

<!--

var userOS = getOS();
var browserName = getBrowserName();
var browserVersion = getBrowserVersion();

if(browserVersion < 4 || browserName == "UNKNOWN" || userOS == "Win16")
{
document.write("<h2>Sorry this browser version is not supported</h2>")
}
else if (browserName == "NETSCAPE")
{
location.replace("webpages/basichome.htm");
}
else
{
location.replace("webpages/home.htm");
}

//-->
</script>
<noscript>
<h2>This website requires a browser supporting scripting. If this is not available then try <a href="webpages/basichome.htm"> here </a> for a basic site layout or enable scripting !</h2>
</noscript>
</body>
</html>

Hope you can decipher that...

Many thanks Dave..

pyro
07-28-2003, 04:21 PM
Using navigator.userAgent, XP returns itself as NT...

Don't know about AOL, but I'm guessing it identifies itself as another browser -- NN perhaps?

bjoerndalen
07-28-2003, 04:48 PM
ok...

I thought Xp may be classed as NT but wasnt sure..

Regarding AOL again I wasnt sure how it had worked.I hadnt expected it too!!

I 'm using IE v5 and windows 98se and checked it using this. I also checked it using versions of Netscape. ..

I had someone with XP and AOL check it and thats when I was surprised that it worked. ...

Just being curious as to why it worked!!

I was trying to cover as many possibilites within reason. I have stats that show approx 87% of visitors to the site I help run use IE V5+, with AOL being the next popular with 8% and Netscape coming in at 3% or so. The rest is made up from a number of different sources.

Best wishes Dave