Skip to content

JavaScript Navigator

The window.navigator object contains information about the visitor’s browser and operating system.

The window.navigator object can be accessed directly as navigator.

It provides various properties, but many of them (especially those related to browser name and version) can be misleading and should not be relied upon for robust browser detection. Feature detection is a much more reliable approach.

Some commonly referenced properties:

  • navigator.userAgent: A string containing the browser’s user-agent header.
  • navigator.language: A string representing the preferred language of the user, usually the language of the browser UI.
  • navigator.languages: An array of strings representing the user’s preferred languages, sorted by preference.
  • navigator.cookieEnabled: A boolean indicating whether cookies are enabled in the browser.
  • navigator.onLine: A boolean indicating whether the browser currently has an internet connection.
  • navigator.platform: A string identifying the platform the browser is running on (e.g., “Win32”, “MacIntel”, “Linux x86_64”). This can also be spoofed.
Section titled “Cookie Enabled Status (navigator.cookieEnabled)”

The navigator.cookieEnabled property returns true if cookies are enabled in the browser, and false otherwise.

// Assuming an HTML element with id="cookieStatus"
// const cookieStatusElement = document.getElementById("cookieStatus");
// if (cookieStatusElement) {
// cookieStatusElement.textContent = "Cookies Enabled: " + navigator.cookieEnabled;
// }
console.log("Cookies Enabled: " + navigator.cookieEnabled);

The navigator.userAgent property returns the user-agent string sent by the browser to the server.

// const userAgentElement = document.getElementById("userAgentInfo");
// if (userAgentElement) {
// userAgentElement.textContent = "User Agent: " + navigator.userAgent;
// }
console.log("User Agent: " + navigator.userAgent);

Warning: The user-agent string is notoriously complex and can be easily spoofed by users or browser extensions. Relying on it to detect specific browsers, versions, or features is highly discouraged. Use feature detection instead.

Legacy Browser Identification Properties (Generally Unreliable)

Section titled “Legacy Browser Identification Properties (Generally Unreliable)”

Older properties like navigator.appName, navigator.appCodeName, navigator.appVersion, and navigator.product exist for historical reasons but provide little reliable information about modern browsers.

For example:

  • navigator.appName often returns “Netscape” for compatibility reasons, even in modern browsers like Chrome, Firefox, and Safari.
  • navigator.appCodeName often returns “Mozilla” for similar historical reasons.

It is best to avoid using these properties for any critical logic.

The navigator.platform property returns a string identifying the platform on which the browser is running.

// const platformElement = document.getElementById("platformInfo");
// if (platformElement) {
// platformElement.textContent = "Platform: " + navigator.platform;
// }
console.log("Platform: " + navigator.platform);

Like userAgent, this information can also be spoofed and should be used with caution.

Browser Language (navigator.language and navigator.languages)

Section titled “Browser Language (navigator.language and navigator.languages)”

The navigator.language property returns a string representing the preferred language of the user (e.g., “en-US”, “fr-FR”).

The navigator.languages property returns an array of strings representing the user’s preferred languages, in order of preference. This is generally more comprehensive.

// const languageElement = document.getElementById("languageInfo");
// if (languageElement) {
// languageElement.textContent = "Preferred Language: " + navigator.language +
// "; All Preferred: " + navigator.languages.join(', ');
// }
console.log("Preferred Language: " + navigator.language);
console.log("All Preferred Languages: " + navigator.languages.join(', '));

The navigator.onLine property returns a boolean value indicating whether the browser is currently connected to the network (true) or not (false).

This property reflects the browser’s understanding of the network state and can be updated dynamically. You can also listen to online and offline events on the window object.

// const onlineStatusElement = document.getElementById("onlineStatus");
// function updateOnlineStatus() {
// if (onlineStatusElement) {
// onlineStatusElement.textContent = "Online: " + navigator.onLine;
// }
// console.log("Online: " + navigator.onLine);
// }
// updateOnlineStatus();
// window.addEventListener('online', updateOnlineStatus);
// window.addEventListener('offline', updateOnlineStatus);

Note: navigator.onLine only indicates if the browser can access the network (e.g., has a network interface active). It doesn’t guarantee connection to a specific server or the internet itself.