The HTML target attribute specifies where to open a linked document.
Definition and Usage
➔ This attribute can be applied to the following HTML elements.
- <a> and <area>: Specifies where the linked URL will be opened.
- <form>: Specifies where the response will be displayed.
- <base>: Sets the default target for the entire document and can be overridden by individual target attributes.
➔ The target attribute accepts following values:
- _self: (Default) Opens in the same window where the link was clicked.
- _blank: Opens the linked document in a new window.
- _parent: Opens the document in the parent frameset.
- _top: Opens the document in the full body of the window.
- framename: Opens the document in a named <iframe>.
Syntax
//In HTML
<p>
Click the link to Opens the linked document in a new window.
<a href="https://w3context.com"
target="_blank"
id="sample">Visit w3context.com</a>
</p>
//In Javascript
<script>
//let element = document.querySelector("#sample");
//element.target = "_blank";
//element.setAttribute('target', '_blank');
</script>
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML target attribute Example</title>
</head>
<body>
<h3>HTML target attribute Example</h3>
<p>target attribute specifies where to open a linked document.</p>
<p>Click the link to Opens the linked document in a new window. <a href="https://w3context.com" target="_blank"
id="sample">Visit wcontext.com</a></p>
</body>
<script>
//let element = document.querySelector("#sample");
//element.target = "_blank";
//element.setAttribute('target', '_blank');
</script>
</html>
Click on the "Try it Now" button to see how it works.