JavaScript Location Object

The JavaScript location object is a property of the window object. It can be used to control the web page displayed by the browser.


  • hash - The URL anchor part including the leading hash mark if one exists This is the part of the URL that is used to point to point to a particular part of a page where a named anchor is. The hash is the part containing the # sign that points to the particular page location.
  • host - The URL hostname and port. The URL has the host value of The colon and port is only included when specified. The URL has the host value of
  • hostname - The URL hostname section
  • href - The entire URL. The following code will load the home CTDP page:

    location.href = ""

    The following code will display the URL of the current page:


  • pathname - The URL pathname section
  • port - The URL port section.
  • protocol - The URL protocol section including the colon after the protocol name. The values are normally http: or file:. The following JavaScript code may be used to identify the source of the URL.
    switch (window.location.protocol) 
       case "http:":
          document.write("From Web<BR>\n")
       case "file:":
          document.write("From Local computer<BR>\n")
          document.write("Unknown Source<BR>\n")
  • search - The URL query string section. This is the section after and including the question mark.
  • target - The URL link's target name.


  • reload() - The current window document is reloaded. If a value of true is passed to the reload function, the reload is forced to do an HTTP GET of the document. This is not normally done but is useful when you think the server contents may be different from your cache.
  • replace(URL) - Requires a URL as a parameter. It loads the document at the URL on top of the current document. A new entry entry is not placed in the history object.