The JavaScript Diaries: Part 7/Page 2
[previous] [next]
The JavaScript Diaries: Part 7
The window
Object
At the top level of the BOM is the window
object. This object
is comprised of the entire browser window, including the document displayed
within the window. All the other objects are child objects of the window
object. As I mentioned before, objects are containers, and in this case, the
window
object is the container that also holds the other objects.
Think of it as a big toy box and the other objects in the hierarchy are the
toys. The document
object, while being a toy, is also a smaller
toy box inside the bigger window
object. It holds other toys but
is contained within the window
object. (This is not to diminish
the importance of the DOM, of which the document
object is at the
top of its hierarchy. It's crucial in manipulating documents within the browser
window.)
The window
object has several properties, methods, and events.
We'll be looking at the more commonly used ones (which are cross-browser compatible).
Properties | Methods | Event Handlers |
closed defaultStatus frames name opener parent self status top window |
alert() blur() close() confirm() focus() moveBy() moveTo() open() print() prompt() resizeBy() resizeTo() scrollBy() scrollTo() setInterval() clearInterval() setTimeout() clearTimeout() |
onLoad onUnload onFocus onBlur onError onResize |
As you can see in the table above, you have already worked with some of the
window
methods, e.g., alert()
, confirm()
and prompt()
.
Properties
First, we'll take a look at the window
properties. I said previously that a property is a component or part of the object. If we think of a car as an object, a car's properties may include a radio and tires. Properties are data containers, just like variables. Besides being a part of an object, they also give information about the object. One important difference is that properties give information about the window object whereas variables do not. An object holds properties that can be accessed from the outside for use in the overall script.
closed
This property is used to check if a window you had previously opened has been closed. It has a Boolean value: it will return true
if the window is closed; otherwise, its value is false
.
If you write a script that retrieves information from a form in another window, and that window has been closed, it will cause an error in the script. To make sure there's no error, you can write a short script using the closed
property to check if the window is still open. An example of such a script is shown below. Place the following script in the <head>
portion of an HTML document.
<script type="text/javascript"> <!-- var newWin; function popNewWin() { newWin=window.open("","","status,width=200,height=200,top=300,left=300"); } function checkWin() { if (!newWin || newWin.closed) { alert("The window has been closed!"); } else { newWin.focus(); } } //--> </script>
Next, place the following code in the <body>
portion.
<form> <input type="button" name="win" value="Open window" onClick="popNewWin()"><br> <input type="button" name="win" value="Check window" onClick="checkWin()"> </form>
Let's take a look at what's happening here.
checkWin .if/else statement that said:
form element, we created two buttons.
|
When used with the opener
property below, you can check to see if the window that opened it has been closed:
if (window.opener.closed) { ..... }
Basically, it says to check to see if a window opened by the current window has been closed. It knows which window to refer to in the statement.
[previous] [next]
Created: July 22, 2005
URL: