Java Reference
In-Depth Information
you may not get an error, but instead get unexpected results. For example, the following code declares a
variable named defaultStatus, and tries to set the defaultStatus property of the window object to
“Welcome to my website”. However, this won't change the default message in the status bar; instead the
value in the defaultStatus variable will change.
var defaultStatus;
defaultStatus = “Welcome to my website”;
In this situation you need to use a different variable name. This happens because any function or vari-
able you defi ne within the global scope actually gets appended to the window object. Look at this code
as an example:
var myVariable = “Hello, World!”;
alert(window.myVariable);
If you were to execute this code in a browser, the alert window will display the message “Hello, World.”
As with all the BOM objects, you can look at lots of properties and methods for the window object. However,
in this chapter you'll concentrate on the history, location, navigator, screen, and document prop-
erties. All fi ve of these properties contain objects (the history, location, navigator, screen, and
document objects), each with its own properties and methods. In the next few pages, you'll look at each
of these objects in turn and fi nd out how they can help you make full use of the BOM.
The history Object
The history object keeps track of each page that the user visits. This list of pages is commonly called
the history stack for the browser. It enables the user to click the browser's Back and Forward buttons to
revisit pages. You have access to this object via the window object's history property.
Like the native JavaScript Array type, the history object has a length property. You can use this to
fi nd out how many pages are in the history stack.
As you might expect, the history object has the back() and forward() methods. When they are called,
the location of the page currently loaded in the browser is changed to the previous or next page that the
user has visited.
The history object also has the go() method. This takes one parameter that specifi es how far forward
or backward in the history stack you want to go. For example, if you wanted to return the user to the
page before the previous page, you'd write this:
history.go(-2);
To go forward three pages, you'd write this:
history.go(3);.
Note that go(-1) and back() are equivalent, as are go(1) and forward().
Search WWH ::




Custom Search