|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
window Object EventsFinally, the window object can respond to the following events:
Window methods can be placed in either the <BODY> or <FRAMESET> tag of the document. To attach a JavaScript function to the onLoad event, for example, you could use this <BODY> tag: <BODY onLoad=alert(Document download complete!)> Chapter 20 includes an extensive example of how to use JavaScript to create and manipulate windows.
The location ObjectAs mentioned earlier, one of the properties of every window is the location object. This object holds the current URL, including the hostname, path, CGI script arguments, and even the protocol. Table 19.1 shows the properties and methods of the location object.
Listing 19.1 shows an example of how you access and use the location object. First, the current values of the location properties are displayed on the Web page (see Figure 19.2). As you can see, not all of them are defined. Additionally, when the button is clicked, the location.href property is set to the URL of my home page. This causes the Web browser to load that page (see Figure 19.3). Listing 19.1 location.htmThe location Object Enables You to Access and Set Information About the Current URL <HTML> <HEAD> <SCRIPT LANGUAGE=JavaScript> <!-- Hide this script from incompatible Web browsers! --> function gohome() { location.href=<http://www.rpi.edu/~odonnj/>; } // Hide this script from incompatible Web browsers! --> </SCRIPT> <TITLE>The Location Object</TITLE> </HEAD> <BODY BGCOLOR=#FFFFFF> <H1>The Location Object</H1> <HR> <SCRIPT LANGUAGE=Javascript> <!-- Hide this script from incompatible Web browsers! --> document.writeln(Current location information:); document.writeln(<UL>); document.writeln(<LI>location.href= + location.href); document.writeln(<LI>location.protocol= + location.protocol); document.writeln(<LI>location.host= + location.host); document.writeln(<LI>location.hostname= + location.hostname); document.writeln(<LI>location.port= + location.port); document.writeln(<LI>location.pathname= + location.pathname); document.writeln(<LI>location.hash= + location.hash); document.writeln(<LI>location.search= + location.search); document.writeln(</UL>); // Hide this script from incompatible Web browsers! --> </SCRIPT> <FORM NAME=Form1> <INPUT TYPE=button NAME=Button1 VALUE=Goto JODs Home Page! onClick=gohome()> </FORM> <HR> <A HREF=mailto:odonnj@rpi.edu><EM>Jim ODonnell</EM></A> </BODY> </HTML>
The history ObjectThe Web browser also maintains a list of pages that you have visited since running the program; this list is called the history list, and it can be accessed through the history object. Your JavaScript programs can move through pages in the list by using the properties and functions shown in Table 19.2.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |