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

JS Window ऑब्जेक्ट

Global window object किसी घर के ढांचे जैसा है: browser page में बाकी हर चीज़ इसके अंदर रहती है, और वे चीज़ें भी जो इससे जुड़ी नहीं दिखतीं (global variables, global functions) असल में चुपचाप इससे लटकी होती हैं।
Syntax
javascript
window.innerWidth;
window.open(url);
window.setTimeout(function, ms);

Window Object क्या है?

window object खुद browser tab या frame को represent करता है और यह Browser Object Model (BOM) में top-level object है।

किसी script में declare किया गया हर global variable और function अपने आप window की एक property बन जाता है, और DOM manipulation के लिए इस्तेमाल होने वाला document object खुद window.document ही है।

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

javascript
let globalVar = "hello";
console.log(window.globalVar); // "hello" - globals become window properties
console.log(window.document === document);

Window Dimensions

window.innerWidth और window.innerHeight viewport size को pixels में बताते हैं, scrollbar शामिल करते हुए पर address bar जैसे browser chrome को छोड़ते हुए।

ये आमतौर पर load पर और resize event पर पढ़े जाते हैं ताकि ऐसा responsive JavaScript व्यवहार बनाया जा सके जिसे अकेले CSS media queries express नहीं कर सकते।

उदाहरण: Window Dimensions

javascript
console.log(window.innerWidth, window.innerHeight);

Windows खोलना और बंद करना

window.open() एक नया browser tab या popup window launch करता है और एक reference return करता है जिसे आप programmatically control कर सकते हैं, जबकि window.close() script द्वारा खोली गई किसी window को बंद करता है।

ज़्यादातर modern browsers window.open() calls को block कर देते हैं जो click जैसी किसी सीधी user action से शुरू नहीं होतीं, एक popup-abuse defense के तौर पर।

उदाहरण: Opening and Closing Windows

javascript
const popup = window.open("", "popup", "width=300,height=200");
popup.document.write("Hello from popup");
// popup.close();

Global Scope के रूप में Window

किसी browser (non-module) script में, top level पर this और implicit global object दोनों window में resolve होते हैं, यही वजह है कि var से declared पुराने style के global variables window की properties बन जाते हैं।

यह समझना सीधे इससे जुड़ता है कि scope और hoisting कैसे काम करते हैं, क्योंकि global execution context असल में window का variable environment ही है।

उदाहरण: Window as the Global Scope

javascript
var globalStyle = "old-style"; // var at top level
console.log(window.globalStyle); // "old-style" - becomes a window property

Practical Window Usage

असली code शायद ही कभी window को सीधे छूता है सिवाय कुछ आम कामों के: responsive JS के लिए viewport size पढ़ना, page को jump कराने के लिए window.scrollTo() इस्तेमाल करना, या current URL के लिए window.location जांचना।

explicitly window का इस्तेमाल आमतौर पर एक संकेत है कि आप pure logic के बजाय कुछ environment-specific करने वाले हैं।

उदाहरण: Practical Window Usage

javascript
console.log(window.innerWidth); // viewport size
window.scrollTo(0, 0);
console.log(window.location.href); // current URL
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. यह मान लेना कि window.open() हमेशा काम करता है, जबकि browsers ऐसे popups block करते हैं जो user click से trigger न हुए हों।
  2. innerWidth (scrollbar शामिल) को document.documentElement.clientWidth से confuse करना।
  3. var से globals बनाना और यह देखकर हैरान होना कि वे window की properties बन जाती हैं।
🔒

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.