Javascript Reference - Location Object








The location object is part of the window object and is accessed through the window.location property.

The location object contains information about the current URL.

<!DOCTYPE HTML> 
<html> 
    <body> 
        <script> 
            document.writeln("<pre>"); 
            document.writeln("protocol: " + document.location.protocol); 
            document.writeln("host: " + document.location.host); 
            document.writeln("hostname: " + document.location.hostname); 
            document.writeln("port: " + document.location.port); 
            document.writeln("pathname: " + document.location.pathname); 
            document.writeln("search: " + document.location.search); 
            document.writeln("hash: " + document.location.hash); 
            document.write("</pre>"); 
        </script> 
    </body> 
</html><!--from   ww  w.j a  va2s  . c o  m-->

Click to view the demo





Location Object Properties

Property Description
hash Sets or gets the anchor part (#) of a URL
host Sets or gets the hostname and port number of a URL
hostname Sets or gets the hostname of a URL
href Sets or gets the URL
origin Returns the protocol, hostname and port number of a URL
pathname Sets or gets the path name of a URL
port Sets or gets the port number of a URL
protocol Sets or gets the protocol of a URL
search Sets or gets the query string of a URL




Location Object Methods

Method Description
assign() Loads a new document
reload() Reloads the current document
replace() Replaces the current document with a new one