Ajax JavaScript CSS HTML 5 Firefox

Navigator in JavaScript, to identify the browser of the user

This is available on browsers since version 1.0 of JavaScript, but some properties and methods have been added later.

Properties and methods of navigator

Standard Properties are recognized by all browsers... Take care to the case sensibility.

appName
The generic name of the class of the browser. Netscape for Firefox.
appCodeName
Name of the browser.
appVersion
Platform (windows, etc..) and version of the browser.
UserAgent
String sent to the server on which the page is loaded.
platform
Code of the operating system, for example win32.

Other properties are recognized only by Internet Explorer or IE and Opera. Their interest is very minimal. They are:

The mimeTypes property is recognized by Mozilla only and returns an array of MIME types supported by the browser.

Methods of navigator:

javaEnabled()
Returns true if Java is enabled and applets can work, false otherwise.

taintEnabled() is not standard, returns true if the option is enabled in which case scripts can deliver information on the system with a security risk.

Getting the version number of the browser

The commercial version number is in appVersion on some browsers, but rather we do use userAgent that contains it on all of them.

It can be extracted from userAgent through the name of the browser with the indexOf method of the String object. These strings are used to identify the browser and retrieve this number.

Firefox/3.0.7
MSIE 7.0
Chrome/1.0.154.48
Opera/9.64
Version/4.0 Safari/528.16

The script:

var ua = navigator.userAgent;


var x = ua.indexOf("MSIE");
var y = 4;
if (x == -1)
{
	x = ua.indexOf("Firefox");
	y = 7;
	if(x == -1)
	{
		if(x == -1)
		{
			x = ua.indexOf("Chrome");
			y = 6;
			if(x == -1)
			{
				x = ua.indexOf("Opera");
				y = 5;
				if(x == -1)
				{
					x = ua.indexOf("Safari");
					if( x != -1)
					{
						x = ua.indexOf("Version");
						y = 7;
					}
				}
			}
		}
	}
}

if(x != -1) 
{
	y ++;
	ua = ua.substring(x + y);
	x = ua.indexOf(" ");
	var x2 = ua.indexOf("(");
	if(x2 > 0 && x2 < x) x = x2;
	x2 = ua.indexOf(";");
	if(x2 > 0 && x2 < x) x = x2;
	if (x == -1) document.write("Error");

	var v = ua.substring(0, x);
	document.write("Version: " + v);
}
    

The script may be used on any web page.

Demonstrations

Getting the version from userAgent

Code above. See manual in link.

Checking the navigator object on any browser

Navigator properties and their values, whatever the source and version of your browser, shown below and used to verify the compatibility of the object.

Code for standard properties:

document.write("navigator.appName: " + navigator.appName);
document.write("navigator.appCodeName: " + navigator.appCodeName);
document.write("navigator.appVersion: " + navigator.appVersion);
document.write("navigator.platform: " + navigator.platform);
document.write("navigator.cookieEnabled: " + navigator.cookieEnabled);
document.write("navigator.userAgent: " + navigator.userAgent);
document.write("navigator.javaEnabled(): " + navigator.javaEnabled());

Reference

© 2009-2012 Xul.fr