JavaScript window.navigator – Navigator Object
14 April 2025 | Category: JavaScript
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
Property | Description |
---|---|
navigator.appName | Returns the browser name |
navigator.appVersion | Returns browser version info |
navigator.userAgent | Returns user agent string (browser + OS info) |
navigator.platform | Returns user’s operating system |
navigator.language | Returns browser language (e.g., en-US ) |
navigator.onLine | Returns true if online |
navigator.cookieEnabled | Returns 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. UseuserAgent
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
Task | Code Example |
---|---|
Get browser info | navigator.userAgent |
Check platform | navigator.platform |
Check if online | navigator.onLine |
Check cookies | navigator.cookieEnabled |
Get browser language | navigator.language |
Get geolocation | navigator.geolocation.getCurrentPosition() |