Get to know Location object from Javascript
Location object
Location object has the location information about the loaded document. location object also provides the general navigation functionality.
The location object is a property of both window and document.
Both window.location
and
document.location
point to the same object.
The locaction object has the following properties and methods:
Name | Description | Returns |
---|---|---|
assign(URL) | Navigates to the specified URL. | void |
hash | Gets or sets the hash component of the document URL, for example "#myID". | string |
host | Gets or sets the host component of the document URL, for example "www.java2s.com:80". | string |
hostname | Gets or sets the host name component of the document URL, for example "www.java2s.com". | string |
href | Gets or sets the current document's location, full URL of the loaded page, for example "http://www.java2s.com". | string |
pathname | Gets or sets the path component of the document URL, for example "/Book/HTML-CSS/". | string |
port | Gets or sets the port of the document URL, for example "8080". | string |
protocol | Gets or sets the protocol of the document URL, for example "http:".. Typically "http:" or "https:" | string |
reload() | Reloads the current document. | void |
replace(URL) | Removes the current document and navigates to the one specified by the URL. | void |
resolveURL(URL) | Resolves the specified relative URL to an absolute one. | string |
search | Gets or sets the query component of the document URL, for example "?q=javascript". | string |
toString | Returns the full URL of the loaded page | string |
To get information about the location of the current object.
<!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><!-- w ww.java2s. c om-->