Anchor href attribute
Definition and usage
href
The attribute sets or returns the link's href attribute value.
href
The attribute defines the destination of the link.
Example
Example 1
Change the target (URL) of the link:
document.getElementById("myAnchor").href = "https://www.codew3c.com/";
Example 2
Get the URL of the link:
var x = document.getElementById("myAnchor").href;
Example 3
Another example of how to get the URL of a link (relative URL):
var x = document.getElementById("myAnchor").href;
Syntax
Return the href attribute:
anchorObject.href
Set the href attribute:
anchorObject.href = URL
Attribute value
Value | Description |
---|---|
URL |
Specifies the URL of the link. Possible values:
|
Technical Details
Return Value: | String value, representing the URL of the link. Returns the entire URL, including the protocol (such as http://). |
---|
Browser Support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support |
Related Pages
HTML Reference Manual:HTML <a> href Attribute Value