JavaScript Navigator

Summary: in this tutorial, you will learn about the JavaScript Navigator object and its properties.

Introduction to the JavaScript Navigator object

The JavaScript Navigator provides information about the web browser and its capabilities. You can reference the Navigator object via the read-only window.navigator property.

The Navigator object has properties that convey the browser’s information. For example, the userAgent is a property of the window.navigator object. It is a long string that identifies the web browser.

window.navigator.userAgent
Code language: CSS (css)

In Google Chrome, you may see the following output:

"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"
Code language: JSON / JSON with Comments (json)

Note that the userAgent may be a little bit different, depending on the Google Chrome version.

The different web browser provides specific capabilities which are not standardized. It’s better not to use the userAgent to identify the web browser because some web browsers allow users to modify the userAgent to pretend they are using a different browser.

For example, you may use the following code to detect if the current web browser is Internet Explorer:

if(navigator.userAgent.includes('MSIE')) {
    // IE, use specific features of IE
} else {
    // not IE
}
Code language: JavaScript (javascript)

To use a specific feature of a web browser, you can use the capability detection. For example:

if( typeof window.addEventListener === 'function' ) {
    // let's use addEventListener
} else {
    // addEventListener is not supported, use another way
}
Code language: JavaScript (javascript)

JavaScript Navigator Properties & Methods

The following table illustrates the JavaScript Navigator properties and methods:

Property / MethodDescription
activeVrDisplaysReturns an array of every VRDisplay instance with its presenting property is set to true
appCodeNameReturns “Mozilla” even in non-Mozilla browsers.
appNameReturns the full browser name.
appVersionReturns the browser version. However, it typically does not correspond to the actual version of the browser.
batteryReturns a BatteryManager object to interact with the Battery status API
buildIdReturn the build number for the web browser.
connectionReturns a NetworkInformation object to interact with the Network information API
cookieEnabledReturns true if if cookies are enabled; otherwise false.
credentialsReturns a CredentialsContainer to interact with the Credentials Management API
deviceMemoryReturns the amount of device memory in gigabytes.
doNotTrackReturns the user’s preference of do-not-track .
geolocationReturns a Geolocation object to interact with the Geolocation API.
getVRDisplays()Returns an array of every VRDisplay instance if available.
getUserMedia()Returns the stream associated with the available media device hardware.
hardwareConcurrencyReturns the number of processor cores of the device
javaEnabledDetermines if Java is enabled in the browser.
languageReturns the browser’s primary language.
languagesReturns an array of all the browser’s preferred languages.
locksReturns a LockManager object to interact with the Web Locks API.
mediaCapabilitiesReturns a MediaCapabilities object to interact with the Media capabilities API
mediaDevicesReturns the available media devices.
maxTouchPointsReturns the maximum number of supported touch points for the device’s touchscreen
mimeTypesReturns an array of MIME types registered with the browser.
onLineSpecifies if the browser is connected to the Internet.
oscpuThe operating system (OS) and/or CPU on which the browser is running.
permissionsReturns the Permissions object to interact with the Permissions API.
platformReturns the system platform on which the browser is running.
pluginsReturns an array of installed browser’s plug-ins.
productReturns the name of the product.
productSubReturns the extra information about the product.
registerProtocolHandler()Registers a website as a handler for a particular protocol.
requestMediaKeySystemAccess()Returns a Promise which resolves to a MediaKeySystemAccess object.
sendBeacon()Asynchronously transmits a small payload.
serviceWorkerReturns the ServiceWorkerContainer used to interact with ServiceWorker object
share()Calls the current platform’s native sharing mechanism.
storageReturns the StorageManager object to interact with the Storage API.
userAgentRepresents the user-agent string of the browser.
vendorReturns the brand name of the browser.
vendorSubReturns extra information about the browser’s vendor.
vibrate()Triggers the device to vibrate if vibration is supported.
webdriverDetermines if the browser is currently controlled by automation.

 

Was this tutorial helpful ?