Window Navigator Javascript
Javascript Navigator Object
The Javascript Navigator is an object of the window
interface, it provides information about the web browser and the operating system. It can be used to detect the browser and operating system of the user.
The Navigator object is accessed using window.navigator
or simply navigator
.
It is a read-only property. The types of information that can be retrieved using navigator
objects are:
- Weather user is online
- The operating user is using (platform)
- Browser user is using
- Browser Version
- Weather Cookies are enabled or not
- Browser Language
- And many more...
JavaScript Navigator Properties
Here is the list of Javascript Navigator properties:
Property | Description |
---|---|
navigator.appCodeName | Returns the code name of the browser. |
navigator.appName | Returns the name of the browser. |
navigator.appVersion | Returns the version information of the browser. |
navigator.cookieEnabled | Returns true if cookies are enabled, otherwise false. |
navigator.geolocation | Returns a Geolocation object that can be used to locate the user's position. |
navigator.language | Returns the language of the browser. |
navigator.onLine | Returns true if the browser is online, otherwise false. |
navigator.platform | Returns for which platform the browser is compiled. |
navigator.product | Returns the engine name of the browser. |
navigator.userAgent | Returns the user-agent header sent by the browser to the server. |
Detect online offline
We can check the internet connection status of the browser and can tell whether the browser is online or offline.
To check the online status of browsers use navigator.online
, it returns a boolean value. It will return true
if the user is online and false
if the user is offline.
Example
function check() {
// check if online
var onlineStatus = navigator.onLine;
if (onlineStatus)
console.log("Browser is online");
else
console.log("Browser is offline");
}
check();
Try it
Note: navigator.online
is not trustable, in some cases it always returns true. Refer to MDN for browser compatibility
Detect Operating System in Javascript
To get the operating system of a user you can use the platform
as well as appVersion
. Both properties result in the operating system of the user in different formats.
Don't rely on these properties to always return the correct result.
Example
function check() {
var os_by_appVersion = navigator.appVersion;
var os_by_platform = navigator.platform;
console.log("OS by appVersion property :" + os_by_appVersion);
console.log("OS by platform property : " + os_by_platform);
}
check();
Try it
Get Browser Name and Version using Javascript
Using the navigator
object we can access many information related to the browser.
- appName - Returns the name of the browser
- appCodeName - The returns code name of the browser
- userAgent - Returns user agent string for the current browser. It includes all information regarding the browser and operating system
Example
function check() {
var appname = navigator.appName;
var appcodename = navigator.appCodeName;
var user_agent = navigator.userAgent;
console.log("App name : " + appname);
console.log("App code Name : " + appcodename);
console.log("User Agent information : "+ user_agent);
}
check();
Try it
Check Cookie Enabled Javascript
To check whether the cookie is enabled or not use cookieEnabled
property. It returns a boolean value true if the cookie is enabled and false if the cookie is disabled.
Example
function check() {
var cookie = navigator.cookieEnabled;
if (cookie)
console.log("Cookie is Enabled");
else
console.log("Cookie is Disabled");
}
check();
Try it
Check Language
To get the language of a webpage using the language
property of the navigator
object. While there are other property languages
that return value in the array.
Example
function check() {
var lang = navigator.language;
var langs = navigator.languages;
console.log("navigator.language - " + lang + "navigator.languages :");
for (let i = 0; i < langs.length; i++) {
console.log(langs[i]);
}
}
check();
Try it