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.


Description

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 stringThe full URL of the page, including the protocol (like https://).


More Examples

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

location.href = "#top";
Try it Yourself »

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


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.