Link href attribute

Definition and usage

href Set or return the URL of the linked document attribute.

See also:

HTML Reference Manual:HTML <link> href Attribute

HTML Reference Manual:HTML <link> Tag

Example

Example 1

Return the URL of the linked document:

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

Try it yourself

Example 2

Change stylesheet:

document.getElementById("myLink").href = "style2.css";

Try it yourself

Syntax

Return href attribute:

linkObject.href

Set href attribute:

linkObject.href = URL

Attribute value

Value Description
URL

Specifies the URL of the linked resource/document.

Possible values:

  • Absolute URL - Points to another website (for example, href="http://www.example.com/default.html")
  • Relative URL - Points to a file within the website (such as href="default.html").

Technical Details

Return Value: A string value representing the URL of the linked document. 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