HTML DOM Link hreflang Property


The Link hreflang property sets/returns the language code of a linked document.

Syntax

Following is the syntax −

  • Returning hreflang attribute value
linkObject.hreflang
  • Setting hreflang to a language code
linkObject.hreflang = langCode

Example

Let us see an example for Link hreflang property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Link hreflang</title>
<link id="extStyle" hreflang='en' rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form>
<fieldset>
<legend>Link-hreflang</legend>
<h3>Spanish</h3>
<input type="button" onclick="changeStyle()" value="In what language is linked document written?">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var extStyle = document.getElementById("extStyle");
   function changeStyle(){
      if(extStyle.hreflang === 'en'){
         divDisplay.textContent = 'The linked document is written in english';
         document.getElementsByTagName('h3')[0].textContent = 'English'
      }
   }
</script>
</body>
</html>

In the above example ‘style.css’ contains −

form {
   width:70%;
   margin: 0 auto;
   text-align: center;
}
* {
   padding: 2px;
   margin:5px;
}
input[type="button"] {
   border-radius: 10px;
}
h3 {
   font-size: 72px;
   color:gold;
}

Output

This will produce the following output −

Before clicking ‘In what language is linked document written?’ button −

After clicking ‘In what language is linked document written?’ button −

Updated on: 30-Jul-2019

81 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements