Javascript Reference - HTML DOM Anchor hreflang Property








The hreflang attribute specifies the language of a linked document.

The hreflang property sets or gets the value of the hreflang attribute of a link.

Browser Support

hreflang Yes Yes Yes Yes Yes

Syntax

Return the hreflang property:

var v = anchorObject.hreflang 

Set the hreflang property:

anchorObject.hreflang = languagecode;




Property Values

Value Description
languagecode A two-letter language code for the linked document.

Return Value

A String representing the language code of the linked document.

Example

The following code shows how to get the language code of a link.


<!DOCTYPE html>
<html>
<body>
<!--   w  w w. jav  a2 s. c  o m-->
<p><a id="myAnchor" hreflang="en-us" href="http://www.example.com/">example</a></p>

<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myAnchor").hreflang;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the language code of a link.


<!DOCTYPE html>
<html>
<body>
<!--from  w  ww .  j a v a2  s . c  om-->
<p><a id="myAnchor" hreflang="en-us" href="http://www.example.com/">example</a></p>

<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("myAnchor").hreflang = "fr";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: