Learn the properties and Methods of the Location Object.
The window object property of location is used to access the URL of the document currently loaded in the window and also provides information about the general navigation functionality of the document.
Its uniqueness lies in the fact that it is a property of both window and document object, hence window.location and document.location points to the same object.
The location object can even parse the current URL into discrete segments which then can be accessed and manipulated using a series of properties.
|hash||The URL hash, the anchor part.||"#myData"|
|host||The servername and port number.||"www.tutorialspark.com:80"|
|hostname||Server name without the port number||"www.tutorialspark.com"|
|href||The full URL of the page currently loaded.||"http://www.tutorialspark.com"|
|port||The port number||"8080"|
|protocol||The protocol currently in use.||"http:"|
|search||The query string of the URL, beginning with a question mark.||"?q=tutorials"|
All types of data related to the location object are easily available, but the query string requires some effort, as there is no way to access it immediately like others.
In the below demo, a function is created to provide easy access to properties of location object involving search query.
|assign(<url>)||To Navigate to the specified URL.||void|
|replace(<url>)||Removes the current URL and navigates to the specified URL||void|
|reload()||To reload the current Document.||void|
|resolveURL(<url>)||To resolve the current URL to the specified one.||string|
The location object provides some methods to manipulate the location object.
The Location object method assign() is used to pass in an URL, follow the example below.
To re-request the page from the server, you need to pass a Boolean value of true to the method.