Javascript Reference - HTML DOM Area href Property








The href property sets or gets the value of the href attribute of an area.

Browser Support

href Yes Yes Yes Yes Yes

Syntax

Return the href property:

var v = areaObject.href

Set the href property:

areaObject.href = URL;

Property Values

Value Description
URL Specifies the hyperlink target for the area.




Return Value

A String representing the URL of the link.

Example

The following code shows how to change the URL of a link in an area.


<!DOCTYPE html>
<html>
<body>
<!--from   w  ww . j  a v a 2s.  com-->
<img src="http://java2s.com/style/demo/border.png" 
     width="145" height="126" usemap="#myImageMap">

<map name="myImageMap">
  <area id="myArea" shape="circle" coords="124,58,8" alt="myImage" href="http://example.com">
</map>
<p id="demo"></p>

<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    document.getElementById("myArea").href = "new.htm";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the URL of a link in an area.


<!DOCTYPE html>
<html>
<body>
<!--from   www  .  java 2s  .co  m-->
<img src="http://java2s.com/style/demo/border.png" width="145" 
     height="126" usemap="#myImageMap">

<map name="myImageMap">
  <area id="myArea" shape="circle" coords="124,58,8" alt="myImage" href="http://example.com">
</map>
<p id="demo"></p>

<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    var x = document.getElementById("myArea").href;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:

Example 3

The following code shows how to change the URL of a link in an area to an absolute URL.


<!DOCTYPE html>
<html>
<body>
<!--  w w  w .  j  a  va 2s.c  o m-->
<img src="http://java2s.com/style/demo/border.png" 
     width="145" height="126" usemap="#myImageMap">

<map name="myImageMap">
<area id="myArea" shape="circle" coords="124,58,8" 
      alt="myImage" href="http://example.com">
</map>
<p id="demo"></p>

<button onclick="myFunction()">test</button>

<script>
function myFunction()
{
    document.getElementById("myArea").href="http://www.your fake server.com";
    document.getElementById("demo").innerHTML="changed";
}
</script>

</body>
</html>

The code above is rendered as follows: