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:
windowname.closed
For example:
<input type="button"
<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>
value="Is the Window ‘ExforsysWindow’ Closed?"
onclick="funcheck()" />
</body>
</html>
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
The Window is ‘ExforsysWindow’
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:
‘ExforsysWindow’ is closed!
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:
‘ExforsysWindow’ is not closed and is Open!
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:
‘ExforsysWindow’ is closed!
for example
<input type="button"
<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>
value="What’s the name of ‘createdWindow’?"
onclick="displayWindow()" />
</body>
</html>
In the above example, the program first creates a window createdWindow and then a button is displayed with text as:
What’s the name of ‘createdWindow’
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:
The Name of window is: Exforsys
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.
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:
window.defaultStatus=textmessage
For example:
window.defaultStatus = "Exforsys -Example of Status bar message to show defaultStatus property"
A complete program as example to understand the usage of this property:
<html>
<head>
<script type="text/javascript">
window.defaultStatus="Exforsys -Example of Status bar
message to show defaultStatus property"
</script>
</head>
</html>
The output of the above script is
Exforsys -Example of Status bar message to show defaultStatus property
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:
window.status=textmessage
For example:
window.status = "Exforsys -Example of Status bar message to show status property"
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:
<html>
<head>
<script type="text/javascript">
<A HREF="http://www.exforsys.com"
onMouseOver="window.status=’Exforsys -Example of
Status bar message to show status property’;
return true">Place Mouse and See Status Bar</A>
</script>
</head>
</html>
The output of the above script is
Exforsys -Example of Status bar message to show status property
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.
window.self
An example to understand this concept:
<html>
<head>
<script type="text/javascript">
function checkwindow()
{
if (window.top!=window.self)
{
document.write("The window is not current
window!!!!!")
}
else
{
document.write("The window is current window!!!!!")
}
}
</script>
</head>
<body>
<input type="button" onclick="checkwindow()"
value="Check the Window by clicking Here">
</body>
</html>
In the above example a button is displayed with message
Check the Window by clicking Here
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:
The window is current window!!!!!
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:
The window is not current window!!!!!
is displayed.