Horje
Web API URL.pathname Property

The Web API URL.pathname property is used to get USVString containing an initial “/” followed by the path of the URL.

Syntax:

var str = URL.pathname

Return Value: This property returns a USVString containing the pathname of the URL.

Note: If there is no path set for the URL, so it will Return only /.

Example 1: There is no path set for the URL, so it will return only /

HTML

<!DOCTYPE html>
<html>
  
<body>
    <h1>GeeksforGeeks</h1>
  
    <button onclick="get()">
        Click on Me!
    </button>
  
    <script type="text/javascript">
        function get() {
            var url = new URL(
        '/tag');
  
            // There is no path for the URL,
            // so it will return only /
            console.log("pathname of current URL is :",
                url.pathname);
        }
    </script>
</body>
  
</html>

Output:

Example 2:

HTML

<!DOCTYPE html>
<html>
  
<body>
    <h1>GeeksforGeeks</h1>
  
    <div id="abc"></div>
  
    <br><br>
    <button onclick="get()">
        Click on Me!
    </button>
  
    <script type="text/javascript">
        function get() {
            var url = new URL(
  
            a = document.getElementById("abc");
      
            a.innerHTML = "pathname of current URL is : "
                + url.pathname;
        }
    </script>
</body>
  
</html>

Output:

Supported Browsers:

  • Safari
  • Opera
  • Chrome
  • Edge
  • Firefox



Reffered: https://www.geeksforgeeks.org


HTML

Related
SVG LinearGradientElement.y2 Property SVG LinearGradientElement.y2 Property
Web API URL.port Property Web API URL.port Property
Web API URL.protocol Property Web API URL.protocol Property
SVG operator Attribute SVG operator Attribute
SVG numOctaves Attribute SVG numOctaves Attribute

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
10