Live, Learn, and Dream

A place to hold things that inspire, amaze, inform, and/or educate

Tag Archives: Programming

Javascript – Browser Detection

I have been very busy lately. Here is something that I wrote a month ago. It is in javascript and can be used to detect browsers and browsers’ versions. The standard or general method is to use navigator.userAgent, and you all know how ‘useful’ it is. I know many (if not millions) people should know and have done it before. However, I couldn’t find anything that I can steal, so that I decided to write it myself and posted the code. One limitation: It can only detect 4 browsers.

function getBrowserInfo(){
var browserInfo = "Your browser is "; 
var browserVersion = navigator.userAgent;
var strArray = browserVersion.split(" ");
	
if(navigator.appName == "Microsoft Internet Explorer"){
	browserVersion = strArray[(3)];
	browserInfo += "IE: " + browserVersion.substring(0, browserVersion.length-1);
	browserInfo += "
For better compatibility, use Chrome or Firefox."; } else if(navigator.appName == "Netscape" && browserVersion.indexOf("rv") != -1){ browserVersion = strArray[(strArray.length - 1)]; strArray = browserVersion.split("/"); browserInfo += strArray[0] + ": " + strArray[1]; } else if(navigator.appName == "Netscape" && navigator.vendor == "Google Inc."){ browserVersion = strArray[(strArray.length - 2)]; strArray = browserVersion.split("/"); browserInfo += strArray[0] + ": " + strArray[1]; } else if(navigator.appName == "Opera"){ browserVersion = strArray[0]; strArray = browserVersion.split("/"); browserInfo += strArray[0] + ": " + strArray[1]; } else{ browserInfo += "Other"; } document.getElementById("browserInfo").innerHTML = browserInfo + "

"; }

I know and have seen a more comprehensive way to do it. Here is my quick and dirty way, and I hope you find it useful. :)
Note: WordPress appears to have problems displaying the code even if I use the code tag. However, I’ll figure it out later.

KKai / 016

Advertisements
%d bloggers like this: