Open In App

Web API URL.pathname Property

Last Updated : 27 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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(
        'https://www.geeksforgeeks.org');
  
            // 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


Similar Reads

Node.js URL.pathname API
The url.pathname is an inbuilt application programming interface of class URL with in url module which is used to get and set the pathname portion of the URL. Syntax: const url.pathname Return value: It gets and sets the pathname portion of the URL. Below programs illustrate the use of url.pathname Method: Example 1: C/C++ Code // node program to d
1 min read
How to Get URL pathname in Next.js?
Next.js is a React framework used to build sophisticated full-stack web applications. Since the introduction of the App router, Next.js's popularity has skyrocketed, making it the most widely used React.js framework. When developing applications, we frequently require URL pathnames to enable different features for specific users. In this article, w
3 min read
Node.js urlObject.pathname API
With the help of urlObject.pathname() method, we can find the name of the path which is used by the given hostname. This contains all the things starting from the host (with the port) and before the beginning of the query or hash components, which are delimited by one of the ASCII question mark (?) or hash (#) characters. Syntax: urlObject.pathname
1 min read
HTML DOM Location pathname Property
The Location pathname property in HTML is used to set or return the pathname of a URL. The Location pathname property returns a string that represents the pathname of the URL. Syntax: It returns the pathname property.location.pathnameIt is used to set the pathname property.location.pathname = pathExample: Below program illustrates the Location path
1 min read
HTML DOM Area pathname Property
The Area pathname Property in HTML DOM is used to set or return the pathname of the href attribute. It is used to specify the destination of the link in an area. Syntax: It returns the Area pathname property. areaObject.pathnameIt is used to set the Area pathname property.areaObject.pathname = path Property Values: It contains a single value path t
2 min read
HTML DOM Anchor pathname Property
The Anchor pathname Property in HTML DOM is used to set or return the path name part of the href attribute value. Syntax: It returns the pathname property. anchorObject.pathnameIt is used to set the pathname property. anchorObject.pathname = pathProperty Values: It contains the value i.e. path which specify the path name of the URL. Return Value: I
2 min read
Web API URL.search Property
The Web API URL.search property is used to get USVString which is a search string or a query string. This string contains a "?" followed by the parameters of the URL. Syntax: var str = URL.search Return Value: This property returns a USVString search string. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;h1&gt;GeeksforGee
1 min read
Web API URL.protocol Property
The Web API URL.protocol property is used to get USVString containing the protocol scheme of the URL. Syntax: var str = URL.protocol Return Value: This property returns a USVString containing the protocol scheme of the URL. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;h1&gt;GeeksforGeeks&lt;/h1&gt; &lt;button onclick=
1 min read
Web API URL.port Property
The Web API URL.port property is used to get USVString containing the port number of the URL. Syntax: var str = URL.port Return Value: This property returns a USVString containing the port number of the URL. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;h1&gt;GeeksforGeeks&lt;/h1&gt; &lt;button onclick=&quot;get()&quot;
1 min read
Web API URL.username Property
The Web API URL.username property returns a USVString containing the username specified before the domain name of the URL Syntax: var str = URL.username Return Value: This property returns a USVString containing the username specified before the domain name of the URL. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;h1&gt;
1 min read