89Chapter 8Window and Document ObjectsThe window.prompt()method (Dedicated web hosting) has two
89Chapter 8Window and Document ObjectsThe window.prompt()method has two parameters. The first is the message that acts as aprompt to the user. You can suggest a default answer in the text field by including a string asthe second parameter. If you don t want any default answer to appear, include an emptystring (two double quotes without any space between them). This method returns one value when the user clicks either button. A click of the Cancel but- ton returns a value of null, regardless of what the user types into the field. A click of the OKbutton returns a string value of the typed entry. Your scripts can use this information in con- ditions for ifand if…elseconstructions. A value of nullis treated as falsein a condi- tion. It turns out that an empty string is also treated as false. Therefore, a condition caneasily test for the presence of real characters typed into the field to simplify a condition test, as shown in the following fragment: var answer = prompt( What is your name? , ); if (answer) { alert( Hello, + answer + ! ); } The only time the alert()method is called is when the user enters something into theprompt dialog box and clicks the OK button. onload event handlerThe windowobject reacts to several system and user events, but the one you will probablyuse most often is the event that fires as soon as everything in a page finishes loading. Thisevent waits for images, Java applets, and data files for plug-ins to download fully to thebrowser. It can be dangerous to script access to elements of a document object while thepage loads because if the object has not loaded yet (perhaps due to a slow network connec- tion or server), a script error results. The advantage of using the onloadevent to invokefunctions is that you are assured that all document objects are in the browser s documentobject model. Window event handlers are placed inside the
tag. Even though you willcome to associate the tag s attributes with the documentobject s properties, it is thewindowobject s event handlers that go inside the tag. The location ObjectSometimes an object in the hierarchy represents something that doesn t seem to have thekind of physical presence that a window or a button does. That s the case with the locationobject. This object represents the URL loaded into the window. This differs from the docu- mentobject (discussed later in this lesson) because the document is the real content; thelocation is simply the URL. Unless you are truly Web-savvy, you may not realize a URL consists of many components thatdefine the address and method of data transfer for a file. Pieces of a URL include the protocol(such as http:) and the hostname (such as www.example.com). You can access all of theseitems as properties of the locationobject. For the most part, though, your scripts will beinterested in only one property: the hrefproperty, which defines the complete URL. Setting the location.hrefproperty is the primary way your scripts navigate to other pages: location.href = http://www.dannyg.com ;