home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next



NOTE:  Each of these methods—as well as the eventsy in the following section—are applied to the window object to which they belong. For instance, blur() or self.blur() would remove the focus from the window in which the document was located. MyWindow.blur() would remove it from the window called MyWindow.

window Object Events

Finally, the window object can respond to the following events:

  onBlur—Triggered when the focus is removed from the window.
  onError—Triggered when an error occurs in the window.
  onFocus—Triggered when the focus is applied to the window.
  onLoad—Triggered when the Web browser finishes loading a document into the window.
  onUnload—Triggered when the user exits from the document within the window.

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.

  See “JavaScript Windows Example,” p. 498.

The location Object

As 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.

Table 19.1 The location Object Contains Information on the Currently Displayed URL
Properties

Name What It Does

href contains the entire URL, including all the subparts; for example, http://www.rpi.edu:80/~odonnj/index.html
protocol contains the protocol field of the URL, including the first colon; for example, http:
host contains the hostname and port number; for example, www.rpi.edu:80
hostname contains only the hostname; for example, www.rpi.edu
port contains the port number; for example, 80
path contains the path to the actual document; for example, ~odonnj/index.html
hash contains any CGI arguments after the first # in the URL
search contains any CGI arguments after the first ? in the URL
Method

assign(string) sets location.href to the value you specify

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.htm—The 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 JOD’s Home Page!”
      onClick=“gohome()”>
</FORM>
<HR>
<A HREF=“mailto:odonnj@rpi.edu”><EM>Jim O’Donnell</EM></A>
</BODY>
 </HTML>


NOTE:  The document.write() method is discussed later in this chapter, in the “The document Object” section.

The history Object

The 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.


FIGURE 19.2  Manipulating the location object gives you another means of moving from one Web page to another.


FIGURE 19.3  By setting its href property, you can use the location object to change the URL your Web browser is looking at.


Previous Table of Contents Next


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.