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
The href property is supported in all major browsers.
Syntax
Return the href property:
Set the href property:
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.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
Color Picker