The Location Object belongs to the object Window and has the following properties:
- href – full query string for the resource
- pathname – resource path
- origin – general query schema
- protocol – protocol
- port – the port used by the resource
- host – host
- hostname – hostname
- hash – if the query string contains a pound sign (#), then this property returns the part of the string that comes after this character
- search – if the query string contains a question mark (?), for example, then this property returns the part of the string that comes after the question mark
For example, we get the address of the current page:
console.log(location.href);
The Location Object also provides a number of methods that can be used to manipulate by request:
- assign(url) – loads a resource located at the path url
- reload(forcedReload) – reloads the current web page. The forcedReload parameter specifies whether to use the browser cache. If the parameter is true, then the cache is not used
- replace(url) – replaces the current web page with another resource located at the url path. Unlike the assign method, which also loads a web page from another resource, the replace method does not store the previous web page on the history stack, so we won’t be able to call the history.back() method to navigate to it.
To redirect to another resource, we can use both properties and methods of location:
location = "http://google.com"; location.href = "http://google.com"; location.assign("http://google.com");
To another local resource:
location.replace("index.html");