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/";

Try it yourself

Example 2

Get the URL of the link:

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

Try it yourself

Example 3

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

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

Try it yourself

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:

  • Absolute URL - Points to another website (for example href="http://www.example.com/index.htm")
  • Relative URL - Points to a file within a website (such as href="index.htm")
  • Anchor URL - Points to an anchor in the page (such as href="#top").

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