HTML DOM Anchor port Property

HTMLWeb DevelopmentFront End Technology

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−


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


 Live Demo

<!DOCTYPE html>
<h1>Demo heading</h1>
<p><a id="mylink" hreflang="en" href="">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>
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;


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

Published on 19-Jun-2019 10:29:24