JavaScript DOM Window Object
In this JavaScript tutorial, you will learn about closed property and name property, defaultStatus, status, self property of JavaScript Window object
closed Property of Window Object:
The closed property of a Window object returns a Boolean variable denoting whether window has been closed or not. The closed property tells you whether or not a window opened using window.open(). Once a window is opened in JavaScript, its closed property is immediately initialized, with a value of false. When the newly opened window is closed, then the closed property of this window is set to a value of true. A programmer can study whether a window is closed or open using the closed property of a Window object.
General syntax of closed property of Window Object:
|
For example:
<html> <head> <script type="text/javascript"> function callClosed() { document.write("’ExforsysWindow’ is closed!") } function callopen() { document.write("’ExforsysWindow’ is not closed and is Open!") } function funcheck() { if (ExforsysWindow.closed) callClosed() else callopen() } </script> </head> <body> <script type="text/javascript"> ExforsysWindow=window.open(”,”,’width=100,height=100′) ExforsysWindow.document.write("The Window is ‘ExforsysWindow’") </script> <input type="button" |
In the above example a new window object ExforsysWindow is opened using window.open statement. The opening of ExforsysWindow gives a window ExforsysWindow with message displayed as
|
A button is created to check whether the window object ExforsysWindow is closed or open. This is done as follows:
On the clicking of this button, the function funcheck() is called. The statement ExforsysWindow. closed is used to check whether the current state of the window ExforsysWindow is open or closed. The value returned by this statement would be false if the window is open and if the window ExforsysWindow is closed, then the value returned by this statement would be true.
If the window ExforsysWindow is closed, the value returned is true. The function callClosed() in if block is called and the statement displayed would be:
|
If the window ExforsysWindow is open, then the value returned is false. The function callopen() in else block is called and the statement displayed would be:
|
name Property of Window Object:
The name property of a Window object is used for setting or returning the name of the window. The name property is a string containing the window’s name.
NOTE: it is not possible to define them dynamically.
General syntax of name property of Window Object:
|
for example
<html> <head> <script type="text/javascript"> function displayWindow() { document.write("The Name of window is: " + createdWindow.name) } </script> </head> <body> <script type="text/javascript"> createdWindow=window.open(”,’Exforsys’,’width=100,height=100′) </script> <input type="button" |
In the above example, the program first creates a window createdWindow and then a button is displayed with text as:
|
When this button is clicked, the function displayWindow() is called. Then the message and the name of the window is displayed on the screen as:
|
Using the function displayWindow() creates Window.name statement in document.write statement. This creates Window.name and has the name of window as Exforsys stored in it.
{mospagebreak title=JavaScript Window Object defaultStatus Property}
JavaScript Window Object defaultStatus Property: The defaultStatus property is a Read/Write property that can be set at any time and defines the default message displayed in a window’s status bar. It is possible to set or return the default text in the status bar of the window using this property. The text is displayed during the loading of the page.
General syntax of defaultStatus property of Window Object:
|
For example:
|
A complete program as example to understand the usage of this property:
|
The output of the above script is
|
The above message appears in the Status bar of the window as default text.
status property of Window Object:
The status property is used to set or return the text in the status bar of a window. For a window, the defaultStatus property reflects the default message displayed in the status bar at the bottom of the window. This defaultStatus property is different from status property. The status property reflects a priority or transient message in the status bar, such as the message that appears when a mouseOver event occurs over an anchor. The defaultStatus property differs to the status property.
In defaultStatus property, the text is always shown in the status bar. The status property can be used to temporarily alter the status bar text. The status property can be used to temporarily change the text displayed in the status bar of a browser window. It is commonly used to set the status bar text when the mouse is moved over links in a document, so that more explanatory text, instead of a URL is displayed.
General syntax of status property of Window Object:
|
For example:
|
The usage looks similar to that of defaultStatus property however, the status property can be set at any time. For example, the programmer can place set the status bar message on the onMouseOver event as follows:
|
The output of the above script is
|
The above message appears in the Status bar of the window only when the user places the mouse over the link displayed as Place Mouse and See Status Bar.
{mospagebreak title=JavaScript Window Object Self Property}
JavaScript Window Object defaultStatus Property: The defaultStatus property is a Read/Write property that can be set at any time and defines the default message displayed in a window’s status bar. It is possible to set or return the default text in the status bar of the window using this property. The text is displayed during the loading of the page.
|
An example to understand this concept:
|
In the above example a button is displayed with message
|
When this button is clicked, the function checkwindow() is called and the Current Window status is checked. If the Window top is equal to window.self (which has the reference to the current window) then else statement gets fired and the message:
|
is displayed.
If the Window top is not equal to window.self (which has the reference to the current window) then if statement gets fired and the message:
|
is displayed.
[catlist id=157]
.