Author Topic: Browser Detection - How to determine browser?  (Read 4883 times)

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
Browser Detection - How to determine browser?
« on: May 24, 2010, 12:07:01 PM »
One rather interesting statistic that you may wish to find out about visitors to your site is which web browser that they are using. Of course, there are a lot of different browsers and many try to pass themselves off as Internet Explorer in order to get scripts on the page that use primitive browser detection scripts instead of feature detection to function properly.

What this means is that many browsers do not identify themselves correctly in the navigator.appname field and so that field is not really useful for detecting which browser is really being used. The following script uses the navigator.userAgent fieldinstead and attempts to determine the correct browser from that. This field can also be set to values that attempt to trick primitive browser detection scripts into thinking that the browser is Internet Explorer instead of whatever browser it really is but the true browser name usually appears somewhere in the field as well and this more advanced browser detection script will only misidentify those browsers that have set out to completely conceal their true identity.

The first step in using this script is to select the code from the text box below and copy it into a file called brsdet.js.
Code: [Select]
// Browser Detection Javascript
// copyright 1 February 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function whichBrs() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'Internet Explorer';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';} else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;
}
 

You next attach the script into your web page that needs to be able to determine which browser is being used by adding the following code into the head section of your page.

Code: [Select]
<script type="text/javascript" src="brsdet.js">
</script>

 Now all that remains is to assign whichBrs() to whichever field that you want to contain the browser name. The statement can be used anywhere within the Javascript in your page. For example you might code it like this:

Code: [Select]
var browserName = whichBrs();
You can use the result from this browser detection script for statistical purposes or where you want to provide different information depending on which browser is being used.

You should not use browser detection to attempt to determine whether a visitor's browser supports particular functionality since there are dozens of different browsers (this script detects at least 15 different ones correctly) and the features provided by the different browsers changes between versions. Instead you should use feature detection if your script requires the use of a particular feature.
 
Also may you check this too:

Code: [Select]
<html>
<body>

<script type="text/javascript">
document.write("Browser CodeName: " + navigator.appCodeName);
document.write("<br /><br />");
document.write("Browser Name: " + navigator.appName);
document.write("<br /><br />");
document.write("Browser Version: " + navigator.appVersion);
document.write("<br /><br />");
document.write("Cookies Enabled: " + navigator.cookieEnabled);
document.write("<br /><br />");
document.write("Platform: " + navigator.platform);
document.write("<br /><br />");
document.write("User-agent header: " + navigator.userAgent);
</script>

</body>
</html>