Anchor href Property
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
The href property is supported in all major browsers.
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:
|
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.w3schools.com/js/
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