?

Log in

No account? Create an account
Browser Name Sniffer - Nothing Is True
Question Yourself

Christopher Parker
Date: 2008-07-30 18:27
Subject: Browser Name Sniffer
Security: Public
Tags:code, javascript, programming, web
NOTE: This code has been updated.

I recently came across a need to sniff a user's browser name with JavaScript. I could just use navigator.appName, but many browsers report Netscape even when that's not really the case (e.g.: Firefox). I've seem some rather inelegant browser name scripts out there. So, I decided to write my own:

var browserName = navigator.appName;

if (browserName != "Microsoft Internet Explorer" || typeof document.all == "undefined")
{
	if (browserName == "Microsoft Internet Explorer")
		browserName = "your browser"; // for user agent switching capability
	else if (browserName != "Netscape" || navigator.userAgent.indexOf("Netscape") != -1)
		browserName = navigator.appName;
	else
	{
		if ((/[ ]([\w]+?)[\/\s][\d\.]+?$/.test(navigator.userAgent))
		 || (/[ ]([\w]+?)[\/\s][\d]+?[^;()]+?$/.test(navigator.userAgent))
		 || (/^([\w]+)[\/\s][\d]+.+/.test(navigator.userAgent)))
			browserName = RegExp.$1;
		else
			browserName = navigator.appName;
	}
}


So far, this has worked most of the time during brief testing. One case where it didn't report the exact name is with Netscape 4.8, which is reported as Mozilla. I'm using Firefox's User Agent Switcher extension to do my testing, and the default Netscape entry that comes with User Agent Switcher is Netscape 4.8, which doesn't contain "Netscape" anywhere in navigator.userAgent.
Post A Comment | Share | Link






browse
my journal
links
November 2008