In JavaScript, the window object represents the browser window or frame in which the current web page is displayed. It provides properties and methods for controlling the browser window or frame, as well as accessing the document object and other objects associated with the current web page.
Here are some examples of using the window object in JavaScript:
Accessing the current URL:
console.log(window.location.href);
Opening a new window:
The window Attributes parameter is important as you display windows because it lets you customize the look of the window you are opening. The window Attributes parameter is optional; not including it gives you a window identical to the current one with respect to attributes.
|
Attribute |
Description |
|
width |
Width of window in pixels |
|
height |
Height of window in pixels |
|
toolbar |
Show/hide browser toolbar |
|
Menubar |
Show/hide browser menu bar |
|
Scrollbars |
Show/hide browser horizontal and vertical scrollbars |
|
resizable |
Allow/disallow resizing of browser window |
|
Status |
Show/hide browser status bar |
|
Location |
Show/hide URL location box |
|
Directories |
Show/hide secondary toolbar (Netscape) |
|
Copyhistory |
Copy current window's Go history for new window |
window.open('https://www.example.com', '_blank');
Closing the current window:
window.close();
Displaying an alert box:
window.alert('Hello, world!');
Setting the title of the current page:
window.document.title = 'My Page Title';
Getting the height and width of the browser window:
console.log('Height: ' + window.innerHeight + ', Width: ' + window.innerWidth);
Redirecting to a new URL:
window.location.href = 'https://www.example.com';
These are just a few examples of how the window object can be used in JavaScript. There are many other properties and methods available on the window object, which can be useful for building dynamic and interactive web applications.