Anchor href Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Anchor

Description

The href property sets or gets the value of the href attribute of a link.

Property Values

Value Description
URL Sets the URL of the link.

Possible values for href property:

  • An absolute URL - points to another web site, like href="http://www.example.com/index.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" or href="http://www.example.com/index.htm#top"

Return Value

A String, representing the entire URL of the link, including the protocol, like http://

The following code shows how to Change the destination (URL) of a link:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p><a id="myAnchor" href="http://www.microsoft.com">www.microsoft.com</a></p>

<button onclick="myFunction()">change the value of the href attribute of the link</button>

<p id="demo"></p>

<script>
function myFunction() {/*ww  w . j  a  va2  s .c o  m*/
    document.getElementById("myAnchor").href = "http://www.cnn.com/";
    document.getElementById("demo").innerHTML = "The link above now goes to www.cnn.com.";
}
</script>

</body>
</html>

Related Tutorials