← Back to JavaScript Course | Chapter 11: Browser Object Model | Lesson 5 of 6

JS Navigator ऑब्जेक्ट

window.navigator एक ऐसे passenger जैसा है जो driver से खुद vehicle के बारे में सवाल पूछ रहा हो, यह कौन सा browser है, इसकी भाषा क्या सेट है, क्या हम अभी online हैं, road (page) के बारे में नहीं जो तय किया जा रहा है।
Syntax
javascript
navigator.userAgent;
navigator.language;
navigator.onLine;

Navigator Object क्या है?

window.navigator browser और user के environment के बारे में जानकारी देता है, जैसे कौन सा browser चल रहा है, यह किस भाषा के लिए configure है, और क्या device के पास अभी network connection है।

उदाहरण: What Is the Navigator Object?

javascript
// 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);

User Agent पढ़ना

navigator.userAgent browser और OS describe करने वाली एक string return करता है, जो historically browser-sniffing logic के लिए इस्तेमाल होती थी, हालांकि modern code को इस नाज़ुक, अक्सर गलत string को parse करने के बजाय feature detection को prefer करना चाहिए।

उदाहरण: Reading the User Agent

javascript
console.log(navigator.userAgent); // prefer feature detection over parsing this

Language और Locale

navigator.language user की preferred language को en-US जैसे एक BCP 47 tag के रूप में बताता है, जो localized content serve करने या visitor की locale preference से मेल खाने के लिए dates/numbers format करने के लिए उपयोगी है।

उदाहरण: Language and Locale

javascript
console.log(navigator.language); // e.g. "en-US"

Online Status

navigator.onLine बताता है कि browser को अभी लगता है कि उसके पास network connectivity है या नहीं, और online/offline events तब fire होते हैं जब वह status बदलता है, जो किसी app को एक connectivity banner दिखाने या connection वापस आने तक actions queue करने देता है।

उदाहरण: Online Status

javascript
// 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"));

अन्य Navigator Capabilities

navigator कई browser APIs का entry point भी है, जिनमें location access के लिए navigator.geolocation (HTML course में ज़्यादा गहराई से कवर किया गया) और system clipboard पढ़ने/लिखने के लिए navigator.clipboard शामिल हैं, इन दोनों को explicit user permission चाहिए।

उदाहरण: Other Navigator Capabilities

javascript
console.log(typeof navigator.geolocation);
console.log(typeof navigator.clipboard); // both require user permission
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. feature detection के लिए navigator.userAgent पर भरोसा करना, क्योंकि इसे fake या change किया जा सकता है; features को सीधे test करें।
  2. यह उम्मीद करना कि navigator.onLine === true का मतलब है internet काम कर रहा है, जबकि यह सिर्फ एक network connection दिखाता है।
  3. logic के लिए navigator.platform जैसी पुरानी properties इस्तेमाल करना, जो deprecated हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.