HTMLAnchorElement: hreflang property
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The hreflang
property of the HTMLAnchorElement
interface is a string that is the language of the linked resource.
It reflects the hreflang
attribute of the <a>
element and is the empty string (""
) if there is no hreflang
element.
Web browsers and search engines may use this information to understand the language of the linked content better, but they are not required to follow it. The value provided for the hreflang
attribute adheres to the format defined in RFC 5646: Tags for Identifying Languages (also known as BCP 47). If not, it is ignored.
Web browsers do not rely solely on the hreflang
attribute after fetching the linked resource. Instead, they use language information directly associated with the resource (e.g., through HTTP headers) to determine its language.
Value
A string that contains a language tag, or the empty string (""
) if there is no hreflang
element.
Example
<a id="exampleLink" href="https://example.com" hreflang="en-IN">Example Link</a>
<p class="hreflang"></p>
#exampleLink {
font-size: 1.5rem;
}
const anchorElement = document.getElementById("exampleLink");
const pTag = document.querySelector(".hreflang");
console.log(anchorElement.hreflang); // Outputs: "en-IN"
pTag.textContent = anchorElement.hreflang;
Result
Specifications
Specification |
---|
HTML Standard # dom-a-hreflang |
Browser compatibility
BCD tables only load in the browser
See also
HTMLLinkElement.hreflang
property