JavaScript Introduction | Browser Object Model | Navigator Object
Navigator Object
The navigator object stores various information about the browser, including browser vendor and version information.
The object’s name comes from Navigator, Netscape’s early web browser.
Browser Sniffing
In the past, sites maintained cross-browser compatibility by identifying a visitor’s web browser type in advance and responding accordingly. This compatibility technique is called browser sniffing.
The navigator object provides various standard and non-standard properties that can be used for browser sniffing.
Today, however, feature testing, which simply tests only the required properties, is used more often than this method.
Name of the Current Browser
The appName and appCodeName properties of the navigator object return the full name of the current browser.
However, for browser compatibility, most browsers use "Netscape" as the browser name in sniffing code.
Most browsers also use "Mozilla" as the browser code name.
document.write("The name of the browser currently in use is " + navigator.appName + ".<br>");
document.write("The code name of that browser is " + navigator.appCodeName + ".");
Internet Explorer 11, Chrome, Firefox, and Safari all use "Netscape" as the browser name.
Internet Explorer 10 and earlier, Chrome, Firefox, Safari, and Opera all use "Mozilla" as the browser code name.
This property has been removed from the web standard, so it is better to avoid using it whenever possible.
Version of the Current Browser
The appVersion and userAgent properties of the navigator object return version information about the current browser as a string.
There is no separately defined standard format for the string returned by these properties.
Therefore, each browser returns a string in a slightly different format.
The value returned by the userAgent property also includes additional detailed information beyond the information in appVersion.
document.write("The version information of the browser currently in use is " + navigator.appVersion + ".<br><br>");
document.write("Additional information available from the userAgent property is " + navigator.userAgent + ".");
This property has been removed from the web standard, so it is better to avoid using it whenever possible.
Operating System Running the Current Browser
The platform property of the navigator object returns a string that identifies the operating system on which the current browser is running.
document.write("The operating system running the current browser is " + navigator.platform + ".");
Default Language Setting of the Current Browser
The language property of the navigator object returns the default language setting of the current browser.
document.write("The default language setting of the current browser is " + navigator.language + ".");
Whether Java Applets Can Run
The javaEnabled() method of the navigator object is a non-standard method that checks whether the current browser can run Java applets.
document.write("The current browser ");
if (navigator.javaEnabled()) {
document.write("can run Java applets.");
} else {
document.write("cannot run Java applets.");
}
Whether Cookies Are Enabled
The cookieEnabled property of the navigator object is a non-standard property that checks whether the current browser can use cookies.
document.write("The current browser ");
if (navigator.cookieEnabled) {
document.write("can use cookies.");
} else {
document.write("cannot use cookies.");
}