Open In App

HTML DOM Anchor hreflang Property

Last Updated : 02 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The DOM Anchor hreflang Property in HTML DOM is used to set or return the value of the hreflang attribute of a URL or a link. The hreflang attribute is used to specify the language for a linked document.

Syntax: 

  • It returns the hreflang property.
anchorObject.hreflang
  • It is used to set the hreflang property.
anchorObject.hreflang = languagecode

Property Values: 

  • languagecode: It specifies the two letter code which represents the language of the linked document.

Return Value: It returns a string value which represents the language for the linked Document.

Example 1: This example returns the hreflang Property. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM Anchor hreflang Property
    </title>
</head>
 
<body style="text-align: center;">
    <h1>GeeksforGeeks</h1>
 
    <h2>DOM Anchor hreflang Property</h2>
 
    <p>Welcome to
        <a href="https://www.geeksforgeeks.org"
            id="GFG" rel="nofollow"
            hreflang="en-us" target="_self">
            GeeksforGeeks
        </a>
    </p>
 
    <button onclick="myGeeks()">
        Submit
    </button>
 
    <p id="sudo"></p>
 
    <script>
        function myGeeks() {
            let x = document.getElementById("GFG").hreflang;
            document.getElementById("sudo").innerHTML = x;
        }
    </script>
</body>
 
</html>


Output:

anchor-hreflang

Example 2: This example sets the hreflang Property. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML DOM Anchor hreflang Property
    </title>
</head>
 
<body style="text-align: center;">
    <h1>GeeksforGeeks</h1>
 
    <h2>DOM Anchor hreflang Property</h2>
 
    <p>Welcome to
        <a href="https://www.geeksforgeeks.org"
            id="GFG" rel="nofollow"
            hreflang="en-us" target="_self">
            GeeksforGeeks
        </a>
    </p>
 
    <button onclick="myGeeks()">
        Submit
    </button>
 
    <p id="sudo"></p>
 
    <script>
        function myGeeks() {
            let x = document.getElementById("GFG")
                .hreflang = "fr";
 
            document.getElementById("sudo").innerHTML
                = "The value of the hreflang attribute" +
                " was changed to " + x;
        }
    </script>
</body>
 
</html>


Output:

anchor-hreflang-2

Supported Browsers:

  • Google Chrome
  • Internet Explorer 10.0 +
  • Firefox
  • Opera
  • Safari


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads