It is used to fetch information of the current URL. The Location object allows you to navigate to another webpage as well.
Suppose we want to get the current website's hostname then we can use the
hostname property of the location object. See the below example:
There are many different properties of the location object and many methods too.
Here are some commonly used properties for the location object:
|protocol||Represents a String at the begining of a URL to the first colon(:),which specifies the Method of access to the URL , for instance http: or https:|
|port||Represents a string specifying the communication port that the server uses, for instance 80|
|pathname||Represents a String Portion of a URL, specifying how a particular resource can be accessed, for instance: order.cgi|
|search||Represents a string beginning with a question mark that specifies any query information in an HTTP URL, for instance batch=1|
|hash||Represents a string beginning with a hash(#), which specifies an anchor name in an HTTP URL, for instance #intro|
Let's take an example and get to know about the properties of Location object.
Now let's see the methods of the location object.
Location Object methods refers to the functions created inside the location interface which can be used to perform various operations on the URL like reload it, change it, etc.
||Loads a new Document in the Browser|
||Reloads the current document that is contained in location.href property.|
||Replaces the current document with the specified new one. In addition, you cannot navigate back to the previous document using the Browser's back button.|
To load a new document, We can use the
assign() method of Location object. But in this example, we have used the
href property and the
replace() methods too. They all can be used to load a document. See the below example: