Home  >  Article  >  Web Front-end  >  HTML DOM Anchor pathname attribute HTML DOM Anchor pathname property returns the pathname portion of the URL

HTML DOM Anchor pathname attribute HTML DOM Anchor pathname property returns the pathname portion of the URL

王林
王林forward
2023-09-07 21:37:011323browse

HTML DOM Anchor pathname attribute is used to set or return the pathname of the href attribute.

The following is the syntax for setting the pathname attribute -

anchorObj.pathname = path

Above, path is the path name of the URL.

The following is the syntax for returning the pathname attribute-

anchorObj.pathname

Now let us see an example of implementing the DOM anchor pathname attribute-

Example

Real-time demonstration

<!DOCTYPE html>
<html>
<body>
<h1>Company</h1>
<p><a id="mylink" hreflang="en" href="https &minus;//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

HTML DOM Anchor pathname属性

HTML DOM Anchor pathname属性返回URL中的路径名部分

Above, click "Show path name" to display the path part-

HTML DOM Anchor pathname attribute

HTML DOM Anchor pathname property returns the pathname portion of the URL

The above is the detailed content of HTML DOM Anchor pathname attribute HTML DOM Anchor pathname property returns the pathname portion of the URL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete