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:

NameDescriptionReturns
assign(URL)Navigates to the specified URL.void
hashGets or sets the hash component of the document URL, for example "#myID".string
hostGets or sets the host component of the document URL, for example "www.java2s.com:80".string
hostnameGets or sets the host name component of the document URL, for example "www.java2s.com".string
hrefGets or sets the current document's location, full URL of the loaded page, for example "http://www.java2s.com".string
pathnameGets or sets the path component of the document URL, for example "/Book/HTML-CSS/".string
portGets or sets the port of the document URL, for example "8080".string
protocolGets 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
searchGets or sets the query component of the document URL, for example "?q=javascript".string
toStringReturns the full URL of the loaded pagestring

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-->

Click to view the demo





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window