Using the location Object's PropertiesLet's put the location object's properties to work in an example. In this case, the code will let the user open a new window, navigate to a new URL in the new window (using the browser's File Open Web Location menu item), and then click a button in the original window to display the details of the new window's location object. In this example, I'll navigate to the URL we saw earlier: http://www.w3.org/TR/html4/appendix/changes.html#19991224 . (I'm choosing this URL because it has a hash part to it.) Usually, browsers won't let you get the properties of the location object in another window, but you can enable the UniversalBrowserRead privilege in the Netscape Navigator to let you do this, so this script is targeted to that browser: (Listing 10-03.html on the web site)<HTML> <HEAD> <TITLE> Using the location Object </TITLE> </HEAD> <BODY> <H1>Using the location Object</H1> <FORM> <INPUT TYPE="BUTTON" ONCLICK="openWindow()" VALUE="Open window"> <INPUT TYPE="BUTTON" ONCLICK="getInfo()" VALUE="Get info"> </FORM> <DIV ID="div1"></DIV> <SCRIPT LANGUAGE="JavaScript"> <!-- var window1 function openWindow() { window1 = window.open("","window1", "height=300, width=300, menubar=yes") } function getInfo() { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead") data = "hash: " + window1.location.hash + "<BR>" data += "host: " + window1.location.host + "<BR>" data += "hostname: " + window1.location.hostname + "<BR>" data += "href: " + window1.location.href + "<BR>" data += "pathname: " + window1.location.pathname + "<BR>" data += "port: " + window1.location.port + "<BR>" data += "protocol: " + window1.location.protocol + "<BR>" document.getElementById("div1").innerHTML = data netscape.security.PrivilegeManager.disablePrivilege( "UniversalBrowserRead") } // --> </SCRIPT> </BODY> </HTML> You can see the results in the Netscape Navigator in Figure 10.3, where we're taking a look at the properties of the location object in the newly opened window. Figure 10.3. Displaying location object properties.
Tip Note that to make this example work, you must navigate to some URL (such as the one I've used here, http://www.w3.org/TR/html4/appendix/changes.html#19991224 ) in the new window it opens; use the File Open Web Location menu item for that. |