Latest web development tutorials
 

Location href Property

< Location Object

Example

Return the entire URL (of the current page):

var x = location.href;

The result of x will be:

Try it Yourself »

More "Try it Yourself" examples below.


Definition and Usage

The href property sets or returns the entire URL of the current page.


Browser Support

Property
href Yes Yes Yes Yes Yes

Syntax

Return the href property:

location.href

Set the href property:

location.href=URL

Property Values

Value Type Description
URL String Specifies the URL of the link.

Possible values:

  • An absolute URL - points to another web site (like location.href="http://www.example.com/default.htm")
  • A relative URL - points to a file within a web site (like location.href="default.htm")
  • An anchor URL - points to an anchor within a page (like location.href="#top")
  • A new protocol - specifies a different protocol (like location.href="ftp://someftpserver.com", location.href="mailto:[email protected]" or location.href="file://host/path/example.txt")

Technical Details

Return Value: A String, representing the entire URL of the page, including the protocol (like http://)

Examples

More Examples

Example

Set the href value to point to another web site:

location.href = "http://www.cnn.com";
Try it Yourself »

Example

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

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

Example

Set the href value to point to an email address (will open and create a new email message):

location.href = "mailto:[email protected]";
Try it Yourself »

< Location Object