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.
hreflang |
Yes | Yes | Yes | Yes | Yes |
Return the hreflang property:
var v = anchorObject.hreflang
Set the hreflang property:
anchorObject.hreflang = languagecode;
Value | Description |
---|---|
languagecode | A two-letter language code for the linked document. |
A String representing the language code of the linked document.
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:
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: