JS Navigator Object
In this page:
navigator.userAgent;
navigator.language;
navigator.onLine;
What Is the Navigator Object?
window.navigator exposes information about the browser and the user's environment, such as which browser is running, what language it's configured for, and whether the device currently has a network connection.
उदाहरण: What Is the Navigator Object?
// Print `navigator.userAgent` to the console
// Print `navigator.userAgent` to the console
console.log(navigator.userAgent);
// Print `navigator.language` to the console
// Print `navigator.language` to the console
console.log(navigator.language);
// Print `navigator.onLine` to the console
// Print `navigator.onLine` to the console
console.log(navigator.onLine);
Reading the User Agent
navigator.userAgent returns a string describing the browser and OS, historically used for browser-sniffing logic, though modern code should prefer feature detection over parsing this fragile, frequently-inaccurate string.
उदाहरण: Reading the User Agent
console.log(navigator.userAgent); // prefer feature detection over parsing this
Language and Locale
navigator.language reports the user's preferred language as a BCP 47 tag like en-US, useful for serving localized content or formatting dates/numbers to match the visitor's locale preference.
उदाहरण: Language and Locale
console.log(navigator.language); // e.g. "en-US"
Online Status
navigator.onLine reports whether the browser currently believes it has network connectivity, and the online/offline events fire when that status changes, letting an app show a connectivity banner or queue actions for when the connection returns.
उदाहरण: Online Status
// Print `navigator.onLine` to the console
// Print `navigator.onLine` to the console
console.log(navigator.onLine);
// Listen for the `offline` event on `window` and run the handler when it fires
// Listen for the `offline` event on `window` and run the handler when it fires
window.addEventListener("offline", () => console.log("Connection lost"));
// Listen for the `online` event on `window` and run the handler when it fires
// Listen for the `online` event on `window` and run the handler when it fires
window.addEventListener("online", () => console.log("Back online"));
Other Navigator Capabilities
navigator is also the entry point for several browser APIs, including navigator.geolocation for location access (covered in more depth in the HTML course) and navigator.clipboard for reading/writing the system clipboard, both of which require explicit user permission.
उदाहरण: Other Navigator Capabilities
console.log(typeof navigator.geolocation);
console.log(typeof navigator.clipboard); // both require user permission
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: