![]() |
| Javascript to Detect Current Browser |
| The following Javascript routine determines the browser name, version and operating system. |
|
|
| This script uses three separate functions. The getBrowser function returns the name of the Browser, i.e. Internet Explorer, Netscape navigator etc.
The getVersion function returns the version number of the browser whilst the getPlatform function determines the operating system the browser is running on.
Lets look at the getBrowser() function first. The navigator.appName property returns the name of the browser. For Netscape Navigator this value is "Netscape" and for Internet Explorer it is "Microsoft Internet Explorer". Use the toLowerCase function to convert all the text to lower case, making the following comparisons easier. The IndexOf function returns the position of the searched string in the agent variable. If -1 is returned then there is no match. |
function getBrowser(){
// indexOf returns position of search string within string being searched
// Set to lowercase for comparisons
var agent=navigator.appName.toLowerCase();
if(agent.indexOf(netscape")>-1)
{return "Netscape Navigator";}
if(agent.indexOf("microsoft internet explorer")>-1)
{return "Microsoft Internet Explorer";}
if(agent.indexOf("opera")>-1)
{return "Opera";}
// unknown browser
return "Unknown";
}
|
| The getVersion() function returns the current version of the browser. As with the getBrowser() function the browser name is stored in lower case with the toLowerCase function.
The parseInt converts the string version number from the navigator.appVersion variable to an integer. Note the quirky way that the browser version is handled in the routine. There is an
additional check for Microsoft Internet Explorer matching the MSIE text of the version details. |
function getVersion(){
// Return the Browser Version
// Get the Browser Name
browserName=navigator.appName.toLowerCase();
// Get the Browser Version
browserVersion=parseInt(navigator.appVersion);
// Now check for differing versions
if (browserName=="netscape" && browserVersion==5)
// Netscape 6 has version number 5
return "Version 6";
else if (browserName=="netscape" && browserVersion==4)
return "Version 4";
else if (browserName=="netscape" && browserVersion==3)
return "Version 3";
else if (browserName=="netscape" && browserVersion==2)
return "Version 2";
else if (browserName=="microsoft internet explorer" && browserVersion==4
&& navigator.appVersion.indexOf("MSIE 6.0") != -1)
// both MSIE 6, 5.5 and 5.0 return browser version of 4
return "Version 6.0";
else if (browserName=="microsoft internet explorer" && browserVersion==4
&& navigator.appVersion.indexOf("MSIE 5.5") != -1)
return "Version 5.5";
else if (browserName=="microsoft internet explorer" && browserVersion==4
&& navigator.appVersion.indexOf("MSIE 5.0") != -1)
return "Version 5.0";
else if (browserName=="microsoft internet explorer" && browserVersion==4)
return "Version 4.0";
else if (browserName=="microsoft internet explorer" && browserVersion<4)
return "Version 3.0";
else if (browserName=="opera" && browserVersion==2)
return "Version 2.0";
else if (browserName=="opera" && browserVersion==3)
return "Version 3.0";
else if (browserName=="opera" && browserVersion==4)
return "Version 4.0";
else if (browserName=="opera" && browserVersion==5)
return "Version 5.0";
// If here then not NN or IE or Opera
else return "Unknown";
}
|
| The final function getPlatform() returns the platform the browser is running on. The indexOf text of>-1 indicates that a match was found. If a value of -1 is returned, as used above, then no match is found. |
function getPlatform(){
// Description - get the Computer operating system
var platform=navigator.appVersion.toLowerCase();
if((platform.indexOf("win16")>-1) || (platform.indexOf("windows 3.1")>-1))
{return "Windows 3.1";}
if((platform.indexOf("windows 98")>-1) || (platform.indexOf("win98")>-1))
{return "Windows 98";}
if((platform.indexOf("windows 95")>-1) || (platform.indexOf("win95")>-1))
{return "Windows 95";}
if((platform.indexOf("windows nt")>-1) || (platform.indexOf("winnt")>-1))
{return "Windows NT";}
if(platform.indexOf("win 9x 4.90")>-1)
{return "Windows ME";}
if(platform.indexOf("windows nt 5.0")>-1)
{return "Windows 2000";}
if(platform.indexOf("windows")>-1)
{return "Windows";}
if(platform.indexOf("macintosh")>-1)
{return "Macintosh";}
if(platform.indexOf("x11")>-1)
{return "Unix";}
if(platform.indexOf("unix")>-1)
{return "Unix";}
return "Unknown";
}
|