The Navigator Object In JavaScript

The Navigator object gives you access to various properties of the viewer’s browser (name, version number etc).
The navigator object is part of the window object which means that you can access its properties and methods
using window.navigator.property_or_method_name.
It can be shortened to navigator.property_or_method_name.

For example instead of window.alert("hi!"); we can have alert("hi!");.

Properties of the navigator object

The properties of the navigator object allows you to find various things about the browser used by the viewers.
These properties can not be changed because they are set to be read-only but you can find what the value of the property is and use it in your script accordingly.

Below is the table of the properties of the navigator object and their returned values.

PropertyValue
AppCodeNameThe code name of the browser.
appNameThe name of the browser.
appMinorVersionA string representing a minor version of the browser (IE only).
appVersionThe version of the browser and some other information.
browserLanguageThe language of the browser being used. (IE and Opera)
buildIDThe build identifier of the browser. (Firefox only)
cookieEnabledSpecifies whether or not the browser has cookies enabled.
cpuClassA string representing the class of the CPU. (IE only)
languageThe language of the browser being used. (Firefox and Opera)
mimeTypesAn array of MIME types supported by the browser.
onLineSpecifies whether or not the browser is in “global offline mode”.
oscpuA string representing the operating system of the computer. (Firefox only)
platformThe machine type for which the browser was created.
pluginsAn array of the plugins the browser has installed on it.
productA string representing the product name of the browser being used. (Firefox only)
productSubThe build number of the browser being used. (Firefox only)
securityPolicyAn empty string-returned a value in Netscape 4.7. (Firefox only)
systemLanguageThe default language used by the operating system. (IE only)
userLanguageThe natural language of the operating system. (IE and Opera)
userAgentThe user agent header for the browser.
vendorAn empty string-returned a string representing the vendor of the browser being used. (Firefox only)
vendorSubAn empty string-returned the vendor version number of the browser being used. (Firefox only)

appCodeName Property

This property holds the value of the application code name of the browser.


window.alert("You are using " + navigator.appCodeName);

Most browsers will return “You are using Mozilla” which was used as the code name for an early version of
Netscape Navigator.

appName Property

The appName property allows you to find out which type of browser the viewer is using to view the page.


window.alert("You are using "+navigator.appName);

Using the appName property you can create a browser detection script that allows you to send the viewer an alert about your opinion of the browser being used.


switch(navigator.appName) {
	case "Netscape": window.alert("Netscape/Firefox is a great browser."); break;
	case "Microsoft Internet Explorer": window.alert("IE is cool"); break;
	case "Opera": window.alert("Opera is a great browser"); break;
	default : window.alert("What browser are you using?");
}

appVersion Property

The appVersion property has a value of the version number of the browser being used and some additional information.
This can be useful when you use techniques that should only be executed in browser versions above a certain level.


window.alert("You are using " + navigator.appVersion);

cookieEnabled Property

The cookieEnabled property returns a Boolean value of true if cookies are enabled in the browser and false if they are not.

onLine Property

The onLine property returns a Boolean value of true if the viewer system is not in “global off-line mode” and false if it is.

platform Property

The platform property holds a value of the type of machine for which the browser was created.


window.alert("You are using " + navigator.platform);

It can be useful if you want to redirect viewers to a different page based on their machine type.

plugins Property

This array holds the values of all plugins installed in the viewer’s browser.

userAgent Property

The userAgent property gives you a long string of information about the browser being used.
This string is the user agent header for the browser.


window.alert("You are using "+navigator.userAgent);

In my case, the code above returns “You are using Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0”.

Methods of the navigator object

The navigator object has a number of methods you can use to perform various tasks. Below is the table of
available methods in the navigator object.

MethodPurpose
javaEnabled()Tests whether or not Java is enabled in the browser.
mozIsLocallyAvailable()Checks if a file at a certain address is locally available (Firefox only).
preference()Allows certain browser preferances to be set (requires signed script).
registerContentHandler()Allows a website to set itself as a potential handler of a certain MIME type.
registerProtocolHandler()Allows a website to set itself as a potential handler of a certain protocol.

javaEnabled() method

The javaEnabled() method checks if the viewer has Java enabled or not in the browser and returns a value of true (if enabled) of false (if not enabled).


var JavaIsOn = navigator.javaEnabled();
if (JavaIsOn == true) {
	window.alert("Java is enabled! That's great!");
}
else {
	window.alert("Java is not enabled! You can't see my Java applet!");
}

Leave a Reply