Javascript Reference - HTML DOM Anchor target Property








The target attribute specifies where to open the linked document.

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

Browser Support

target Yes Yes Yes Yes Yes

Syntax

Return the target property:

var v = anchorObject.target 

Set the target property:

anchorObject.target="blank|_self|_parent|_top|framename"




Property Values

Value Description
_blank Opens the linked document in a new window
_self Opens the linked document in the same frame. Tthis is default
_parent Opens the linked document in the parent frameset
_top Opens the linked document in the full body of the window
framename Opens the linked document in a named frame

Return Value

A String representing where to open the linked document.

Example

The following code shows how to change the target of a link to "_blank".


<!DOCTYPE html>
<html>
<body>
<!--from w w  w.j  a  va  2s.  c om-->
<p><a id="myAnchor" href="http://www.example.com">example</a></p>
<button onclick="myFunction()">test</button>

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

<script>
function myFunction() {
    document.getElementById("myAnchor").target = "_blank";
    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 value of the target attribute of a link.


<!DOCTYPE html>
<html>
<body>
<!--from  w w  w  .  j  a v a 2s.  c o m-->
<p><a id="myAnchor" target="_self" href="http://www.example.com/">example</a></p>
<button onclick="myFunction()">test</button>

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

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

</body>
</html>

The code above is rendered as follows: