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.
target |
Yes | Yes | Yes | Yes | Yes |
Return the target property:
var v = anchorObject.target
Set the target property:
anchorObject.target="blank|_self|_parent|_top|framename"
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 |
A String representing where to open the linked document.
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:
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: