Window Object in JavaScript
By: aathishankaran in JavaScript Tutorials on 2007-04-02
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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments