Window location.href Property

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;

Try it yourself

Example 2

Set the URL of the current page:

location.href = "";

Try it yourself

Example 3

Set the href value to point to an anchor within the page:

location.href = "#top";

Try it yourself

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";

Try it yourself

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