Window location.href
Examples
Get the URL of the current page:
let url = location.href;
Try it Yourself »
Set the URL of the current page:
location.href = "https://www.w3schools.com";
Try it Yourself »
More examples below.
Definition and Usage
The location.href
property sets or returns the entire URL of the current page.
Syntax
Return the href property:
location.href
Set the href property:
location.href = URL
Property Value
Parameter | Description |
URL |
An absolute URL like: http://www.example.com/default.htm A relative URL like default.htm An anchor URL like location.href="#top" A new protocol like: ftp://someftpserver.com mailto:someone@example.com file://host/path/example.txt |
Return Value
Type | Description |
A string | The full URL of the page, including the protocol (like https://). |
More Examples
Set the href value to point to an email address (will open and create a new email message):
location.href = "mailto:someone@example.com";
Try it Yourself »
Browser Support
location.href
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |