How to know the browser language and browser platform in JavaScript?


In this article we are going to discuss how to know the browser language and browser platform with the help of examples in JavaScript

To know the various properties of the browser, the JavaScript provides Navigator object. The Navigator object has several properties, which include - connection, credentials, cookies, geolocation, language, platform, etc. We are concerned with Navigator.language and Navigator.platform to know the language and platform of the browser. Let us discuss about them in detail.

Navigator.language

The read-only Navigator.language property returns a string that represents the browser UI language. It returns a string that returns the language version. For example, ‘en’, ‘en-US’, ‘fr’, ‘fr-FR’, etc.

Example 1

The following is an example program to determine the browser language.

<!DOCTYPE HTML>
<html>
<head>
<title>How to know the browser language and browser platform in JavaScript</title>
</head>
<body style = "text-align:center;">
   <h3>To Know the Language of the browser.</h3>
   <p id="text1"></p>
   <script type="text/javascript">
      document.getElementById("text1").innerHTML = "The Language of the browser is : "+navigator.language;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 2

To know the list of preferred languages set by the user, the property navigator.languages is used. They are displayed in the format of most preferred language to least preferred language by the user. The first element of the returned array is navigator.language. The following example demonstrates the navigator.languages property.

<!DOCTYPE HTML>
<html>
<head>
   <title>How to know the browser languages and browser platform in JavaScript</title>
</head>
   <body style = "text-align:center;">
      <h3>To Know the Languages of the browser.</h3>
      <p id="text1"></p>
      <script type="text/javascript">
         document.getElementById("text1").innerHTML = "The Language of the browser is : "+navigator.languages;
      </script>
   </body>
</html>

On executing the above code, the following output is generated.

Navigator.Platform

The read-only Navigator.platform property returns a string that represents the browser platform. Some of the platforms include ‘MacIntel’, ’MacPPC’, ‘Win32’, ’Win16’, ‘Linux x86_64’, and ’Linux i686’.

Example

The following is an example program to determine the navigator platform.

<!DOCTYPE HTML>
<html>
<head>
   <title>How to know the browser platform in JavaScript</title>
</head>
<body style = "text-align:center;">
   <h3>To Know the Platform of the browser.</h3>
   <p id="text1"></p>
   <script type="text/javascript">
      document.getElementById("text1").innerHTML = "The Platform of the browser is : "+navigator.platform;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Updated on: 08-Dec-2022

494 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements