HTML DOM Anchor hostname Property

HTMLWeb DevelopmentFront End Technology

The HTML DOM Anchor hostname property returns only the hostname of the URL. It returns the domain name.

Syntax

Following is the syntax to set the hostname property −

anchorObj.hostname = hostname

Above, the hostname is the hostname of the URL.

Syntax

Following is the syntax to return the hostname property −

anchorObj.hostname

Example

Let us now see an example to implement the DOM Anchor hostname property −

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h1>Company</h1>
<p><a id="mylink" href="http://www.demo.com:6064/abc/def.html#myteam">Our Team</a></p>
<h2 id="myid"></h2>
<button onclick="display()">Display Anchor Part</button>
<button onclick="display2()">Display Host Part</button>
<button onclick="display3()">Display Hostname</button>
<script>
   function display() {
      var a = document.getElementById("mylink").hash;
      document.getElementById("myid").innerHTML = a;
   }
   function display2() {
      var a = document.getElementById("mylink").host;
      document.getElementById("myid").innerHTML = a;
   }
   function display3() {
      var a = document.getElementById("mylink").hostname;
      document.getElementById("myid").innerHTML = a;
   }
</script>
</body>
</html>

Output

This will produce the following output −

Now, click the hostname button to get the hostname −

raja
Updated on 17-Sep-2019 08:58:40

Advertisements