• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

The location Object

The location object holds information about the location of the current web page, such as its URL, the domain name, path, server port, and more. This object is part of the window object, and you can navigate to a new URL just by assigning that URL to the location object’s href property (or, in most browsers, to the location object itself). Here’s an example; in this case, we’ll let the user enter a URL and make the browser navigate to that URL when the user clicks a button:

(Listing 04-06.html on the web site)

<HTML> 
    <HEAD> 
        <TITLE> 
            Navigate to a URL 
        </TITLE> 
        <SCRIPT LANGUAGE = "JavaScript"> 
            <!--
            function Jump() 
            { 
                window.location.href = document.form1.text1.value 
            } 
            // --> 
        </SCRIPT> 
    </HEAD> 

    <BODY> 
        <H1>Navigate to a URL</H1> 
        <FORM NAME = "form1"> 
            <BR> 
            <INPUT TYPE = TEXT NAME="text1" SIZE = 80> 
            <BR> 
            <BR> 
            <INPUT TYPE = BUTTON VALUE="Navigate to URL" ONCLICK="Jump()"> 
        </FORM> 
    </BODY> 
</HTML> 

					  

You can see this script at work in Figure 4.7.

Figure 4.7. Navigating to an URL.


You’ll find the properties and methods (there are no events) of the location object in Table 4.10. We’ll get all the details on browser version for each property, method, and event in Chapter 10.

Table 4.10. The Properties and Methods of the location Object
Properties Methods
hash assign
host reload
hostname replace
href 
pathname 
port 
protocol 
search 


Tip

The location object is a great one to use when you want to redirect a browser to a new site. We’ll see more on this object in this chapter when we want to redirect a browser to various documents tailored by browser version.


  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint