Web Development Tutorials




  
  

The location Object

The location object is considered part of window object and has properties and methods that parse the URL that's in the browser's address bar. The general format of the window.location object or more simply location is shown below:


location.property

location.method()

Figure 7-15.General format to reference properties of the location object.

For each of the following properties, its value is shown for the browser you are using to view this page. These properties are read and displayed during page load with inline scripts in the format


  document.getElementById("Output").innerHTML = location.property;
Property Description and Current Setting
href The complete URL of the Web page currently loaded in the browser window:
  
protocol The protocol portion of the URL including the colon
  
hostname  The host name portion of the URL:
  
port The port number of the Web server:
  
host The host name and port number:
  
path The path to the Web page:
  
search The query string from the URL:
  
hash The anchor name from the URL:
  

The location object includes the following methods. The (location) method is accessible like the object properties


location.method()

Figure 7-16. Method of the location object.

Property Description and Current Setting
reload(force) Reloads the current Web page. If the parameter is set to true, the browser loads the page from the server rather than from the browser's cache memory.
  location.reload()
replace(url) Loads a new page in the browser and overwrites the current page in the history list
  location.replace()

Redirecting Users

The location object can be used to redirect users to a new Web page:


window.onload = init;

function init()
{
location.href="http://www.mga.edu";
}


TOP | NEXT: Window Timers