Javascript Reference - HTML DOM Link hreflang Property








The hreflang property sets or gets the language code of the linked document.

Browser Support

hreflang Yes Yes Yes Yes Yes

Syntax

Return the hreflang property.

var v = linkObject.hreflang 

Set the hreflang property.

linkObject.hreflang=languagecode

Property Values

Value Description
languagecode Set the language code of the linked document




Return Value

A String type value representing the language code of the linked document.

Example

The following code shows how to change the language code of the linked document.


<!DOCTYPE html>
<html>
<head>
<link id="myLink" href="http://java2s.com" hreflang="en">
</head><!--  w  ww. ja v a 2  s . c o m-->
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myLink").hreflang = "en-us";
}
</script>
</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the language code of the linked document.


<!DOCTYPE html>
<html>
<head>
<link id="myLink" href="http://example.com" hreflang="en">
</head><!--from   ww w. j a va  2  s  .  c o  m-->
<body>
<button onclick="myFunction()">test</button>

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

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

</body>
</html>

The code above is rendered as follows: