Signup/Sign In

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.

Properties Description
appcodename specifies the code name of the browser (experimental property - can return incorrect value)
appname specifies the name of the browser (experimental property - can return incorrect value)
appversion specifies the version of browser being used (experimental property - can return incorrect value)
cookieEnabled specifies whether cookies are enabled or not in the browser
platform contains a string indicating the machine type for which the browser was compiled.
useragent contains a string representing the value of user-agent header sent by the client to server in HTTP protocol
geolocation returns object of GeoLocation which can be used to get the location information of the device.
onLine specifies whether the browser is online or not.
language returns a string with the preferred browser language, for example, en-US for English.
languages returns 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.

Let's see a code example with some of these properties in action.

Navigator Object Methods

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

Method Name Description
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.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight