JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript window.navigator – Navigator Object

14 April 2025 | Category:

The window.navigator object contains information about the browser and the device. It helps you detect the user’s environment — such as their browser, OS, online status, and more.


📌 What is window.navigator?

The navigator object is a property of the window object that provides information about:

  • Browser type and version
  • Platform (OS)
  • User’s language
  • Whether the user is online
  • Whether cookies are enabled
  • Geolocation access

🔹 Common navigator Properties

PropertyDescription
navigator.appNameReturns the browser name
navigator.appVersionReturns browser version info
navigator.userAgentReturns user agent string (browser + OS info)
navigator.platformReturns user’s operating system
navigator.languageReturns browser language (e.g., en-US)
navigator.onLineReturns true if online
navigator.cookieEnabledReturns true if cookies are enabled

🔸 Examples

✅ Detect User Agent (Browser + OS)

console.log("User Agent:", navigator.userAgent);

✅ Detect Platform (Operating System)

console.log("Platform:", navigator.platform);

✅ Check If User is Online

if (navigator.onLine) {
  console.log("User is online");
} else {
  console.log("User is offline");
}

✅ Check Cookie Support

console.log("Cookies Enabled:", navigator.cookieEnabled);

🔍 Detect Browser Language

console.log("Browser Language:", navigator.language);

You can use this to automatically set language preferences on your site.


📍 Use Case: Detect Browser Name

⚠️ navigator.appName returns "Netscape" for most browsers (including Chrome and Firefox), so it’s not reliable. Use userAgent string instead.

console.log("Browser Name:", navigator.appName); // Usually "Netscape"

🌐 Advanced: Geolocation (via navigator.geolocation)

You can get the user’s location (with permission):

navigator.geolocation.getCurrentPosition(function(position) {
  console.log("Latitude:", position.coords.latitude);
  console.log("Longitude:", position.coords.longitude);
});

⚠️ Works only over HTTPS and requires user permission.


✅ Summary

TaskCode Example
Get browser infonavigator.userAgent
Check platformnavigator.platform
Check if onlinenavigator.onLine
Check cookiesnavigator.cookieEnabled
Get browser languagenavigator.language
Get geolocationnavigator.geolocation.getCurrentPosition()