Window location.href Property
- Previous Page hostname
- Next Page origin
- Go Up One Level Window Location
Definition and Usage
location.href
Sets or returns the entire URL of the current page.
Example
Example 1
Get the URL of the current page:
let url = location.href;
Example 2
Set the URL of the current page:
location.href = "";
Example 3
Set the href value to point to an anchor within the page:
location.href = "#top";
Example 4
Set the href value to point to an email address (it will open the software and create a new email):
location.href = "mailto:someone@example.com";
Syntax
Return href attribute:
location.href
Set href attribute:
location.href = URL
Attribute value
Value | Description |
---|---|
URL |
Absolute URL, for example: http://www.example.com/index.html Relative URL, for example: index.html Anchor URL, such as: location.href="#top" New protocols, such as: ftp://someftpserver.com mailto:someone@example.com file://host/path/example.txt |
Return value
Type | Description |
---|---|
String | The complete URL of the page, including the protocol (such as https://). |
Browser Support
All Browsers Support location.href
:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support | Support |
- Previous Page hostname
- Next Page origin
- Go Up One Level Window Location