You are here: Reference > JavaScript > client-side > HTML DOM > objects > location

location object

Browser support:
Contains information about the URL of the currently loaded document, and provides methods to navigate to a new page.
If you need the URLs of the previously visited pages, use the history object.

Syntax:

Properties that reference the object:
object.location
Related objects:
The base interface, through which you can add new functionalities to the location object, is the Location interface.

Possible members:

Properties:
hash
Sets or returns the subsection (begins with a '#' sign) of the URL assigned to the object.
host
Specifies or retrieves the host name and port of the URL assigned to the object.
hostname
Specifies or retrieves the host name of the URL assigned to the object.
href
Specifies or returns the location of the destination.
pathname
Specifies or retrieves the path component of the URL assigned to the object (relative to the host).
port
Sets or retrieves the port component of the URL assigned to the object.
protocol
Sets or retrieves the protocol component of the URL assigned to the object.
search
Sets or returns the subsection (begins with a '?' sign) of the URL assigned to the object.
Methods:
assign
Overrides the location of the current document, so it effectively loads a new document.
reload
Reloads the current page.
replace
Removes the URL of the current document from the document history and loads the document at the specified URL into the current window.

Example HTML code 1:

This example illustrates the use of the location object:
<head>
    <script type="text/javascript">
        function AnalyzeLocation () {
            var message = "";
            message += "hash: " + location.hash + "\n";
            message += "host: " + location.host + "\n";
            message += "hostname: " + location.hostname + "\n";
            message += "href: " + location.href + "\n";
            message += "pathname: " + location.pathname + "\n";
            message += "port: " + location.port + "\n";
            message += "protocol: " + location.protocol + "\n";
            message += "search: " + location.search + "\n";

            alert (message);
        }
    </script>
</head>
<body>
    <button onclick="AnalyzeLocation ();">Analyze the location of the current document</button>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Post Content