Home » JavaScript » JavaScript Window Navigator

JavaScript Window Navigator

The window.navigator object is used for detection of information related to the browser. It gives information about browser like plugins, platform, appCodeName, appVersion, etc.

Although the data provided by the navigator is not always correct. The browser owners can manipulate the data of the navigator. Many browser have same Codenames so people can get confused also. But it can be used wisely and some of the information can be retrieved.


Detect Browser (Online or Offline)

The navigator.onLine property can be used to detect whether the browser is online or offline. It returns Boolean values, i.e. 'true' means online and 'false' means offline.

In the example below, an online condition is tested which will result in any of the two responses from the browser.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Window Navigator Detect Online </title>
</head>
<body>
<script>
function checkConnectionStatus() {
if(navigator.onLine) {
alert("Application is online.");
} else {
alert("Application is offline.");
}
}
</script>
<button type="button" onclick="checkConnectionStatus();">Check Connection Status</button>
</body>
</html>

Output

JavaScript Window Navigator Detect Online

Check Browser Cookie

The navigator.cookieEnabled can be used to check if the cookies are enabled in the browser or not. It also return a Boolean value,i.e. 'true' if cookies are enabled and 'false' if not.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Window Navigator Detect Cookie </title>
</head>
<body>
<script>
function checkCookieEnabled() {
if(navigator.cookieEnabled) {
alert("Cookies are enabled in your browser.");
} else {
alert("Cookies are disabled in your browser.");
}
}
</script>
<button type="button" onclick="checkCookieEnabled();">Check If Cookies are Enabled</button>
</body>
</html>

Output

JavaScript Window Navigator Detect Cookie

Detect Browser Language

The navigator.language property is used to detect the language used in the browser.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Window Navigator Detect Browser Language </title>
</head>
<body>
<script>
function checkLanguage() {
alert("Your browser's UI language is: " + navigator.language);
}
</script>
<button type="button" onclick="checkLanguage();">Check Language</button>
</body>
</html>

Output

JavaScript Window Navigator Detect Browser Language

Navigator Object Methods

Property Description
appName It returns the browser name.
appVersion It returns the browser version.
appCodeName It returns browser's code name
cookieEnabled It checks cookie's status in the browser.
userAgent It returns the user agent.
language It returns the browser's language.
plugins It returns the plugins used in the browser.
mimeTypes[] It returns the array of mime type.
platform It returns the platform, ex. Win32.
onLine It returns the browser's online/offline status.
javaEnabled() Checks if java is enabled.
taintEnabled() Checks if taint is enabled.

Methods of JavaScript navigator object

Below is the example of all the navigator objects together, you can easily understand all of them just by looking at the example. Have a look at it, and read the information about your browser.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Window Navigator Detect Browser Details </title>
</head>
<body>
<script>
function getBrowserInformation() {
var info = "\n App Name: " + navigator.appName;
info += "\n App Version: " + navigator.appVersion;
info += "\n App Code Name: " + navigator.appCodeName;
info += "\n User Agent: " + navigator.userAgent;
info += "\n Platform: " + navigator.platform;
info += "\n JavaScript Enable: " + navigator.javaEnabled();
info += "\n Cookie Enable: " + navigator.cookieEnabled;
info += "\n Language: " + navigator.language;
info += "\n Online: " + navigator.onLine;
info += "\n Plugins: " + navigator.plugins;
alert("Here're the information related to your browser: " + info);
}
</script>
<button type="button" onclick="getBrowserInformation();">Get Browser Information</button>
</body>
</html>

Output

JavaScript Window Navigator Detect Browser Details








Follow Us: