JS Navigator ऑब्जेक्ट
In this page:
navigator.userAgent;
navigator.language;
navigator.onLine;
Navigator Object क्या है?
window.navigator browser और user के environment के बारे में जानकारी देता है, जैसे कौन सा browser चल रहा है, यह किस भाषा के लिए configure है, और क्या device के पास अभी 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);
User Agent पढ़ना
navigator.userAgent browser और OS describe करने वाली एक string return करता है, जो historically browser-sniffing logic के लिए इस्तेमाल होती थी, हालांकि modern code को इस नाज़ुक, अक्सर गलत string को parse करने के बजाय feature detection को prefer करना चाहिए।
उदाहरण: Reading the User Agent
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
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
// 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
console.log(typeof navigator.geolocation);
console.log(typeof navigator.clipboard); // both require user permission
- feature detection के लिए
navigator.userAgentपर भरोसा करना, क्योंकि इसे fake या change किया जा सकता है; features को सीधे test करें। - यह उम्मीद करना कि
navigator.onLine === trueका मतलब है internet काम कर रहा है, जबकि यह सिर्फ एक network connection दिखाता है। - logic के लिए
navigator.platformजैसी पुरानी properties इस्तेमाल करना, जो deprecated हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: