HTML DOM Anchor pathname Property

Front End TechnologyHTMLWeb Development

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

Following is the syntax to set the pathname property −

anchorObj.pathname = path

Above, path is the pathname of the URL.

Following is the syntax to return the pathname property −

anchorObj.pathname

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

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h1>Company</h1>
<p><a id="mylink" hreflang="en" href="https −//abc.com/abc.html/#new">Products</a></p>
<h2 id="myid"></h2>
<button onclick="display1()">Display pathname</button>
<button onclick="display2()">Display hreflang</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;
   }
</script>
</body>
</html>

Output

Above, click on “Display pathname” to display the path part −

raja
Published on 18-Jun-2019 08:56:05

Advertisements