Location object is associated with the address bar of the browser, and its properties contain all read and write URL components.
Location is usually accessed through
Window.location objects. If the script is run from an iframe (on the same domain), the parent window can be accessed through
Let's see what the values will be for the following URL:
Returns the full URL of the page.
location object can be assigned a new URL, the browser will immediately go to the new page.
window.location.href = 'https//snippens.com';
You can also use the
location.replace() methods for redirecting. The difference with the latter is that the user will not be able to use the back button.
Returns the used protocol, including
The port number, if it is not in the URL, then nothing will be displayed.
Contains domain and port (if any).
Contains only the domain.
The path string of the current page, starting with
GET parameters, starting with
The hash of the page, starting with
You don't need to use a hash to change the hash:
window.location.hash = '123';