What is the importance of Navigator Object in JavaScript?



The JavaScript navigator object is used for browser detection, it can be used to get browser information such as appName, appCodeName, userAgent, etc. Navigator object is a window property so it can be accessed by the "window.navigator" or "navigator".

It can also check whether the browser is in online mode or not and whether Java is enabled or not in our system.

There are many properties of navigator objects that return information about the browser following are some of those −

  • appName − Returns the name

  • appVersion − Returns the version

  • appCodeName − Returns the code name

  • cookieEnabled − Returns true if the cookie is enabled otherwise false

  • userAgent − Returns the user agent

  • language − Returns the language. It is supported in Netscape and Firefox only.

  • userLanguage − Returns the user language. It is supported in IE only.

  • plugins − Returns the plugins. It is supported in Netscape and Firefox only.

  • systemLanguage − Returns the system language. It is supported in IE only.

  • mimeTypes[] − Returns the array of mime types. It is supported in Netscape and Firefox only.

  • platform − Returns the platform e.g. Win32.

  • online − Returns true if the browser is online otherwise false.

Example: 1

In the following example, we are checking the few properties whether it is working or not. If you want to check the all properties just only change the properties name.

<!DOCTYPE html>
<html>
<head>
   <title>navigator objet</title>
</head>
<body>
   <script>
      document.writeln("<br/>navigator.appCodeName : " + navigator.appCodeName);
      document.writeln("<br/>navigator.appName : " + navigator.appName);
      document.writeln("<br/>navigator.appVersion : " + navigator.appVersion);
      document.writeln("<br/>navigator.cookieEnabled : " + navigator.cookieEnabled);
      document.writeln("<br/>navigator.language : " + navigator.language);
      document.writeln("<br/>navigator.userAgent : " + navigator.userAgent);
      document.writeln("<br/>navigator.platform : " + navigator.platform);
      document.writeln("<br/>navigator.onLine : " + navigator.onLine);
   </script>
</body>
</html>

Example 2

In the following example, we are checking the method if we have installed the java or not in our pc. But in our pc java is not installed i.e. the output is false.

<!DOCTYPE html>
<html>
<head>
   <title>navigator objet</title>
</head>
<body>
   <script>
      var check = "Is java Enabled in your pc : " + navigator.javaEnabled();
      document.write(check);
   </script>
</body>
</html>

Example 3

The following example demonstrates the importance of the navigator object in JavaScript.

In this case, we will use the navigator object to obtain browser information to check if your browser is in online mode. If so then it displays true if it is in online mode else returns false and it displays the necessary browser information.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8" />
   <meta http-equiv="X-UA-Compatible" content="IE=edge" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <title>JavaScript Navigator Object example</title>
   <div id="check"></div>
</head>
<body>
   <script type="text/javascript">
      let x = "Is your browser is in online mode? " + navigator.onLine;
      document.getElementById("check").innerHTML = x;
   </script>
</body>
</html>

Advertisements