Latest web development tutorials
 

Anchor href Property

< Anchor Object

Example

Change the destination (URL) of a link:

document.getElementById("myAnchor").href = "http://www.cnn.com/";
Try it Yourself »

Definition and Usage

The href property sets or returns the value of the href attribute of a link.

The href attribute specifies the destination of a link.


Browser Support

Property
href Yes Yes Yes Yes Yes

Syntax

Return the href property:

anchorObject.href

Set the href property:

anchorObject.href=URL

Property Values

Value Description
URL Specifies the URL of the link.

Possible values:
  • An absolute URL - points to another web site (like href="http://www.example.com/default.htm")
  • A relative URL - points to a file within a web site (like href="default.htm")
  • An anchor URL - points to an anchor within a page (like href="#top")

Technical Details

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

More Examples

Example

Get the URL of a link:

var x = document.getElementById("myAnchor").href;

The result of x will be:

http://www.w3ii.com/js/default.html
Try it Yourself »

Example

Another example of how to get the URL of a link (a relative URL):

var x = document.getElementById("myAnchor").href;
Try it Yourself »

Related Pages

HTML reference: HTML <a> href attribute


< Anchor Object