Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLAnchorElement: href address and target destination

HTMLAnchorElement represents a hyperlink in the browser DOM. Its href property specifies the resource address, while target selects the browsing context in which to open it. Learn how the href property differs from the attribute and what _blank means.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const anchor = document.createElement("a");
anchor.href = "https://example.com/guide";
anchor.target = "_blank";
anchor.rel = "noopener";

HTMLAnchorElement is the DOM interface for the a hyperlink element and inherits from HTMLElement. Two link properties cover separate decisions: where the link leads (href) and where its resource should appear (target). This is a browser Web API, not part of ECMAScript itself; the repository's Node.js version does not establish its availability.

Setting anchor.href updates the href attribute. On reading, a valid relative address is resolved against the document's base URL, whereas anchor.getAttribute("href") returns the attribute's stored text without that transformation. If the attribute is absent, anchor.href returns an empty string. If the attribute is present but empty, its value is resolved against the document's base URL. With a hierarchical base URL, the result omits any fragment from the base. With an opaque base URL such as a data: URL, resolution can fail, in which case anchor.href also returns an empty string. An invalid URL may be returned unchanged: reading href is not security validation.

anchor.target reflects the target attribute. _self selects the current context, _blank a new one, _parent the parent, and _top the topmost context. Without the relevant ancestor, _parent and _top behave like _self. The value can also identify a named context. _blank does not guarantee a tab rather than a window; browser settings affect that choice.

For _blank, you can explicitly set anchor.rel = "noopener" to prevent access through window.opener; modern browsers also provide this implicitly for _blank. Visible link text should announce the new tab or window. Assigning properties alone does not navigate. MDN marks href and target as widely available since July 2015; this does not imply identical support for every HTMLAnchorElement feature.

Examples

const anchor = document.createElement("a");
anchor.href = "https://example.com/guide";
anchor.target = "_blank";
anchor.rel = "noopener";

console.log(anchor.href);
console.log(anchor.getAttribute("href"));
console.log(anchor.target);

Result

https://example.com/guide
https://example.com/guide
_blank

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources