JavaScript Navigator Object

JavaScript Navigator Object is used to fetch information related to the browser(useragent), like the browser name, browser version, operating system information, etc.

JavaScript Navigator object is also a property of the JavaScript Window object and can be accessed using the read only property window.navigator for the current browser window.

In JavaScript, the Navigator Object includes some properties and methods which help us to navigate from one element to another.

Navigator Object Properties

The properties of the navigator object are the variables created inside the Navigator Object.

We can access Navigator Object Property as: navigator.propertyname where propertyname is the name of property.

PropertiesDescription
appcodenamespecifies the code name of the browser (experimental property – can return incorrect value)
appnamespecifies the name of the browser (experimental property – can return incorrect value)
appversionspecifies the version of browser being used (experimental property – can return incorrect value)
cookieEnabledspecifies whether cookies are enabled or not in the browser
platformcontains a string indicating the machine type for which the browser was compiled.
useragentcontains a string representing the value of user-agent header sent by the client to server in HTTP protocol
geolocationreturns object of GeoLocation which can be used to get the location information of the device.
onLinespecifies whether the browser is online or not.
languagereturns a string with the preferred browser language, for example, en-US for English.
languagesreturns a string with all the languages supported by the browser in order of user preference.

There are some more expreimental properties available in the Navigator object, but the ones mentioned above are most commonly used properties.

Navigator Object Example

    <script>
        let appc =  navigator.appCodeName;
        let appn =  navigator.appName;
        let appv =  navigator.appVersion;
        let appco = navigator.cookieEnabled;
        let lan   = navigator.language;
        let onl = navigator.onLine;
        let pla = navigator.platform;
        let usra = navigator.userAgent;

        document.write(appc +"<br>");
        document.write(appn +"<br>");
        document.write(appv +"<br>");
        document.write(appco +"<br>");
        document.write(lan +"<br>");
        document.write(onl +"<br>");
        document.write(pla +"<br>");
        document.write(usra +"<br>");
    </script>

</body>

Navigator Object Methods

The Navigator object doesn’t have many standardised methods, most of the methods available are experimental.

Method NameDescription
registerProtocolHandler()allows websites to register themselves as protocol handler so that they are allowed to open some standard URL schemes like mailto:tel:sms:, etc,
share()used to utilize the native support of sharing available in browser.
sendBeacon()used to send a small data packet from the client to the server
vibrate()if supported, this can be used to make the device vibrate, if not supported, nothing happens. This is useful in mobile browsers.

Use of Navigator Object:

The navigator object can be used for multiple specific usecases which can help you make your website better, like:

  1. Making you web app or website more compatible with different browsers by writing specific code for different browsers to resolve compatibility issues.
  2. You can check if the browser is online or not, to show some message if the browser is disconnected from internet.
  3. You can use the location information of the user to show location specific content.
  4. You can set the preferred language as the website language if you support multiple languages on your website.

All the points mentioned above can improve the user experience of your website multifold.

3 thoughts on “JavaScript Navigator Object

  1. Pingback: JavaScript Syntax

Leave a comment