Pages

Sep 17, 2009

How to detect browser name + Clientside + javascript

Below is the Clientside script to identify the browser name:

function detect_browser()
{
var agt = navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
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;
}

This is the general method for all browsers , if you want to identify only when
the browser is safari then use like this

function detect_browser()
{
var agt = navigator.userAgent.toLowerCase();
if (agt.indexOf("safari") != -1) return 'Safari';

}

Apart from this the navigator object has the following properties

The navigator object was conceived back in the days when Netscape Navigator reined supreme. These days it serves as much as an irony of NS's diminished market share as way of probing browser information.

The navigator object of JavaScript contains the following core properties:
Properties Description
appCodeName - The code name of the browser.
appName - The name of the browser (ie: Microsoft Internet Explorer).
appVersion - Version information for the browser (ie: 4.75 [en] (Win98; U)).
cookieEnabled - Boolean that indicates whether the browser has cookie enabled.

language - Returns the default language of the browser version (NS and Firefox only).
mimeTypes[] - An array of all MIME types supported by the client. (
NS and Firefox only).
platform[] - The platform of the client's computer (ie: Win32).
plugins - An array of all plug-ins currently installed on the client. (
NS and Firefox only.)
systemLanguage Returns the default language of (ie: en-us). IE only.
userAgent - String passed by browser as user-agent header.

(ie: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1))
userLanguage - Returns the preferred language setting of the user (ie: en-ca).
IE only.

Thanks
Mukunda

No comments: