This object is used to get information about current URL of the browser and automatically navigates the user to another page. This object is part of the window object and can be accessed using the window.location property. There is no public standard that applies to the location object, but all major browsers support it.
The following are the properties of Location object:
- host: It gives host name and domain name of the URL.
- hostname: It gives domain name of the URL.
- href: It specifies URL name.
- pathname: It returns path name of the URL.
- protocol: It specifies the protocol used for the URL such as http:// or https://.
- port: It specifies port of the URL.
- search: It is used to search portion of the URL.
The following are the methods of Location object:
- reload: It reloads the current document.
- replace: It replaces current URL with new URL.
- assign: It loads the new document.
Location Object Example
- window.location.href = “http://www.google.com”; line loads the specified URL as mentioned.
Location Object Demo
- Save the file as JSlocation_example.html in your system.
- Just open the file in the browser, you will see that the page is redirected to http://www.javabeat.net.