-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
The JavaScript Window Object represents the browser window or tab that contains a web page. It’s the top-level object in the Browser Object Model (BOM), meaning all other objects like document
, history
, location
, and navigator
are part of it.
In simple terms, everything that happens in your browser window — from opening new tabs to showing alerts — starts with the window
object.
In JavaScript, the window
object is automatically created when a web page loads. It acts as a global container that holds:
Browser-related information (like screen size, URL, etc.)
Methods (like alert()
, setTimeout()
, etc.)
Properties (like window.innerWidth
, window.location
, etc.)
You don’t have to explicitly write window.
every time — it’s implied by default.
<script>
// Both lines do the same thing
window.alert("Welcome to JavaScript BOM!");
alert("Welcome to JavaScript BOM!"); // 'window' is optional
</script>
Because everything in a browser runs inside a “window,” JavaScript needs a way to:
Access and manipulate the web page (window.document
)
Communicate with the browser (window.navigator
, window.location
)
Manage user interaction (window.alert
, window.confirm
)
Control timing (setTimeout
, setInterval
)
That’s why understanding the window
object is essential for working with the Browser Object Model.
Here are some commonly used properties of the window
object:
Property | Description |
---|---|
window.innerWidth |
Returns the width of the window’s content area (in pixels). |
window.innerHeight |
Returns the height of the window’s content area. |
window.outerWidth |
Returns the full width of the browser window (including toolbars). |
window.outerHeight |
Returns the full height of the browser window. |
window.screenX |
Horizontal position of the window relative to the screen. |
window.screenY |
Vertical position of the window relative to the screen. |
window.location |
Returns the URL (Location object). |
window.document |
Refers to the HTML document loaded in the window. |
<script>
// Display window dimensions
document.write("Inner Width: " + window.innerWidth + "<br>");
document.write("Inner Height: " + window.innerHeight + "<br>");
</script>
The window
object provides many useful methods to perform browser actions.
Displays a popup alert box.
<script>
window.alert("This is an alert message!");
</script>
Shows a popup with OK and Cancel buttons.
<script>
let result = window.confirm("Do you want to continue?");
if(result){
document.write("User clicked OK");
}else{
document.write("User clicked Cancel");
}
</script>
Takes input from the user through a popup dialog box.
<script>
let name = window.prompt("Enter your name:");
document.write("Hello, " + name);
</script>
Opens a new browser window or tab.
<script>
// Opens Google in a new tab
window.open("https://www.google.com", "_blank");
</script>
Closes the current browser window (if it was opened by window.open()
).
<script>
// Close the window (only if opened via script)
window.close();
</script>
You can get or even move and resize the browser window using these methods (though many are restricted for security reasons).
<script>
// Get window size
console.log("Width: " + window.innerWidth);
console.log("Height: " + window.innerHeight);
// Move the window (only works for popups)
window.moveTo(100, 100);
window.resizeTo(800, 600);
</script>
Every global variable and function automatically becomes a property of the window
object.
<script>
var site = "CodePractice.in";
function showSite(){
console.log("Welcome to " + site);
}
// Access them as window properties
console.log(window.site); // "CodePractice.in"
window.showSite(); // "Welcome to CodePractice.in"
</script>
This is why it’s important to avoid polluting the global scope — because all global variables are added to window
.
The window
object also controls time-based actions using two key methods:
Runs a function after a specific delay (in milliseconds).
<script>
// Show message after 3 seconds
setTimeout(() => {
alert("This message appears after 3 seconds");
}, 3000);
</script>
Runs a function repeatedly at specified intervals.
<script>
// Display time every second
setInterval(() => {
let time = new Date().toLocaleTimeString();
document.getElementById("clock").innerHTML = time;
}, 1000);
</script>
<p id="clock"></p>
You can stop the interval using clearInterval()
.
The document
object is part of the window
— it represents the web page content loaded inside the window.
<script>
console.log(window.document.title); // Prints page title
window.document.body.style.backgroundColor = "lightyellow"; // Change background color
</script>
So when you use document.getElementById()
, you’re really using window.document.getElementById()
.
The window
object contains other sub-objects that let you interact with the browser:
window.location
→ Current page URL and navigation.
window.history
→ Access browser history (back/forward).
window.navigator
→ Information about the browser and device.
window.screen
→ Details about the user’s screen resolution.
Each of these will be explained in separate tutorials.
Avoid using too many global variables — they all attach to window
.
Prefer const
or let
for declaring variables to prevent conflicts.
Use modern event handling (addEventListener
) instead of inline onclick
.
Always test window
-related methods on different browsers — some may have restrictions.
Avoid using window.open()
for unnecessary popups (may be blocked).
The JavaScript Window Object is the foundation of the Browser Object Model (BOM). It represents the browser tab or window and provides methods and properties to interact with it — from alerts and timing functions to screen size and location details.
Mastering window
helps you control the browser environment effectively and gives you access to all other BOM features.
How can you display a popup message to the user when a webpage loads using the window.alert()
method?
Write a script that asks the user’s name using window.prompt()
and then displays a greeting with their name.
How can you use window.confirm()
to ask a user if they want to leave a page, and display different messages based on their choice?
Write code to open a new browser window that loads “https://www.google.com” using the window.open()
method.
How can you close a window that was opened using JavaScript? Demonstrate with window.close()
.
Write a program to display the window’s inner width and height on the webpage using window.innerWidth
and window.innerHeight
.
How can you move or resize a browser window using moveTo()
and resizeTo()
methods? (Test this in a popup window.)
Write code to show a digital clock that updates every second using setInterval()
and displays the time inside a <p>
element.
How can you execute a function after 5 seconds using setTimeout()
and display a message when it runs?
Create a global variable and function, then access both using the window
object. Explain how JavaScript treats global variables in the browser.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts