HTML | DOM Anchor href Property

The DOM Anchor href Property in HTML DOM is used to set or return the value of the href attribute of a link. The href attribute is used to specify the link’s destination.

Syntax:

  • It returns the href property.
    anchorObject.href
  • It is used to set the href property.
    anchorObject.href = URL 

Property Values: It contains the value i.e URL which specify the URL of the link.



  • absolute URL: It points to another website.
  • relative URL: It points to a file within a website.
  • anchor URL: It points to an anchor within a page.

Return value: It returns a string value which represents the entire URL of the link including the protocol.

Example-1: This Example returns the href Property.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML DOM Anchor href Property
    </title>
</head>
  
<body>
    <center>
        <h1>
          GeeksForGeeks
      </h1>
  
        <h2>
          DOM Anchor href Property
      </h2>
  
        <p>Welcome to
            <a href=
               id="GFG" 
               rel="nofollow" 
               hreflang="en-us" 
               target="_self"
                GeeksforGeeks 
            </a>
        </p>
  
        <button onclick="myGeeks()">
          Submit
      </button>
  
        <p id="sudo"
           style="color:green;
                  font-size:25px;">
      </p>
  
        <script>
            function myGeeks() {
                
                var x = 
                    document.getElementById(
                      "GFG").href;
                
                document.getElementById(
                  "sudo").innerHTML = x;
            }
        </script> "
    </center>
</body>
  
</html>

chevron_right


Output:
Before Clicking On Button:

After Clicking On Button:

Example-2 : This example sets the href property.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML DOM Anchor href Property
    </title>
</head>
  
<body>
    <center>
        <h1>
          GeeksForGeeks
      </h1>
  
        <h2>
          DOM Anchor href Property
      </h2>
  
        <p>Welcome to
            <a href=
               id="GFG" 
               rel="nofollow" 
               hreflang="en-us" 
               target="_self"
                GeeksforGeeks 
            </a>
        </p>
  
        <button onclick="myGeeks()">
          Submit
      </button>
  
        <p id="sudo"
           style="color:green;
                  font-size:25px;">
      </p>
  
        <script>
            function myGeeks() {
                
                var x =
                    document.getElementById("GFG").href = 
                    "https://ide.geeksforgeeks.org/tryit.php";
                
                document.getElementById("sudo").innerHTML = x;
            }
        </script> "
    </center>
</body>
  
</html>

chevron_right


Output:
Before Clicking On Button:

After Clicking On Button:

Supported Browsers: The browser supported by DOM Anchor href property are listed below:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.