CKTIS / JavaScript
HTMLAnchorElement.target — where a link navigates
Read or set a link’s `target` attribute, and distinguish its stored value from the browsing context the browser selects when the link is followed.
Version: Current web platform; runtime support varies
Syntax
anchor.target
anchor.target = targetNameOrKeyword;HTMLAnchorElement.target is a read/write DOM property on an a element. Reading it returns the text of the target attribute, or the empty string "" if the attribute is absent. Assigning a string sets the attribute; it does not trigger navigation. href supplies the link’s URL, while target says where to open it.
_self selects the current browsing context, _parent selects its parent (or the current context if there is no parent), and _top selects the top-level context. _blank requests a new context, not necessarily a new tab. A name such as report can select an existing frame with that name; if no eligible match is found, the browser may open a new context. Browser settings and sandbox restrictions can affect the outcome.
If the link has no target attribute, navigation uses the value from the first base element with that attribute, if there is one. Without such a default, the ordinary destination is the current context. Explicitly setting anchor.target = '' leaves an empty attribute and bypasses the base default. Use getAttribute('target') to distinguish absent (null) from empty (""), and removeAttribute('target') to restore fallback behavior.
For navigation with a _blank target, the standard applies noopener by default, so the new page does not get access to the opening page through window.opener. rel="opener" opts out of that default protection; explicit rel="noopener" preserves it. This is a web-platform API, not an ECMAScript 2024 feature; a runtime without a DOM does not provide it automatically.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTMLAnchorElement.target</title>
</head>
<body>
<script>
const anchor = document.createElement('a');
anchor.href = '#demo';
const log = () => console.log(JSON.stringify([
anchor.target, anchor.getAttribute('target')
]));
log();
anchor.target = '_blank';
log();
anchor.setAttribute('target', 'report');
log();
anchor.target = '';
log();
anchor.removeAttribute('target');
log();
</script>
</body>
</html>Result
["",null] ["_blank","_blank"] ["report","report"] ["",""] ["",null]
Save the example as an HTML file and open it in a browser. Each line in the browser console shows the property value followed by the attribute value. The code neither inserts nor activates the link.
Related lessons
- JavaScript · Lesson 37 · Advanced · Version: 4