CKTIS / JavaScript
HTMLAnchorElement: link URL and navigation target
`href` specifies the link's address; `target` tells the browser which browsing context to use when following it.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
anchor.href; // Reads the URL property
anchor.href = urlString; // Sets the href attribute
anchor.target; // Reads the target attribute as a string
anchor.target = targetName; // Sets the target attribute
anchor.getAttribute("href"); // Reads the stored value or null
anchor.getAttribute("target"); // Reads the stored value or nullHTMLAnchorElement.href/target refers to two read-write properties on an a element: href sets the address and target selects where to navigate. They are web platform DOM APIs, not part of ECMAScript 2024. Assigning either property updates the corresponding HTML attribute without starting navigation.
Reading anchor.href returns a serialized absolute URL resolved against the document's current base URL, which a base element can set. If the href attribute is absent, the property returns "" and the element does not create a hyperlink. If URL parsing fails, the property returns the attribute value unchanged rather than throwing. A present but empty attribute is an empty relative reference, not a missing address. getAttribute("href") returns the stored value, or null if absent.
anchor.target returns the stored value, or "" when absent, not the effective navigation target. When following the link, the browser can use the default from base if the attribute is absent; without such a default, it uses the current context. _self means the current context, _parent the parent, and _top the topmost. _blank requests a new context, not specifically a tab rather than a window. A name can select an existing named context, such as an iframe.
Under the current HTML standard, _blank implies rel="noopener" unless rel="opener" explicitly opts in to an opener. Explicit rel="noopener" prevents the new context from getting an opener reference through window.opener; rel="noreferrer" also suppresses referrer information.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTMLAnchorElement: href and target</title>
</head>
<body>
<script>
const doc = document.implementation.createHTMLDocument();
const base = doc.createElement("base");
base.href = "https://example.com/course/";
base.target = "_self";
doc.head.append(base);
const anchor = doc.createElement("a");
console.log(JSON.stringify([anchor.href, anchor.getAttribute("href")]));
anchor.href = "lesson-38#demo";
console.log(JSON.stringify([anchor.getAttribute("href"), anchor.href]));
console.log(JSON.stringify([anchor.target, anchor.getAttribute("target")]));
anchor.target = "_blank";
console.log(JSON.stringify([anchor.target, anchor.getAttribute("target")]));
</script>
</body>
</html>Result
["",null] ["lesson-38#demo","https://example.com/course/lesson-38#demo"] ["",null] ["_blank","_blank"]
Save the example as an HTML file, open it in a browser, and check the console output. A separate document fixes the base URL. Compare the properties with the stored attributes: the document's default target does not appear in `anchor.target`. The code does not follow the link.
Related lessons
- JavaScript · Lesson 38 · Advanced · Version: 4