HTML DOM Anchor port Property


The HTML DOM Anchor port property is used to set or return the port of the href attribute.

Following is the syntax to set the port property−

anchorObj.port = num

Above, num is the port number of the url.

Following is the syntax to return the port property−

anchorObj.port

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

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h1>Demo heading</h1>
<p><a id="mylink" hreflang="en" href="https://abc.com:6064/abc.html/#new">Services</a></p>
<h2 id="myid"></h2>
<button onclick="display1()">Display pathname</button>
<button onclick="display2()">Display hreflang</button>
<button onclick="display3()">Display port</button>
<script>
function display1() {
   var a = document.getElementById("mylink").pathname;
      document.getElementById("myid").innerHTML = a;
   }
   function display2() {
      var a = document.getElementById("mylink").hreflang;
      document.getElementById("myid").innerHTML = a;
   }
   function display3() {
      var a = document.getElementById("mylink").port;
      document.getElementById("myid").innerHTML = a;
   }
</script>
</body>
</html>

Output

Above, click on “Display port” to display the port number−

Updated on: 30-Jul-2019

81 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements