Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to get the file name from page URL using JavaScript ?

  • Last Updated : 29 Jan, 2020

Suppose you have given an HTML page and the task is to get the file name of an HTML page with the help of JavaScript. There are two approaches that are discussed below:

Approach 1: In this approach, window.location.pathname returns the relative URL of the page. Use split() method to split the URL on “/” and pop() method to get the last item from the array.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Example: This example implements the above approach.




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            How to get the name of a
            file using JavaScript
        </title>
          
        <style>
            body {
                text-align: center;
            }
            h1 {
                color: green;
            }
            #geeks {
                font-size: 26px; 
                font-weight: bold; 
                color: green;
            }
        </style>
    </head>
      
    <body>
        <h1>GeeksforGeeks</h1>
          
        <p>
            Click on the button to get
            the name of the file.
        </p>
          
        <button onclick="GFG_Fun();">
            click here
        </button>
          
        <p id="geeks"></p>
          
        <script>
            var down = document.getElementById('geeks');
      
            function GFG_Fun() {
                var path = window.location.pathname;
                down.innerHTML = path.split("/").pop();
            }
        </script>
    </body>
      
    </html>
  • Output:

Approach 2: In this approach, location.pathname returns the relative URL of the page. Use lastIndexOf() method to get the last “/” and substring() method to get the item after “/” from the string.

  • Example: This example implements the above approach.




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>
            How to get the name of
            a file in JavaScript
        </title>
          
        <style>
            body {
                text-align: center;
            }
            h1 {
                color: green;
            }
            #geeks {
                font-size: 26px; 
                font-weight: bold; 
                color: green;
            }
        </style>
    </head>
      
    <body>
        <h1>GeeksforGeeks</h1>
          
        <p>
            Click on the button to get
            the name of the file.
        </p>
          
        <button onclick="GFG_Fun();">
            click here
        </button>
          
        <p id="geeks"></p>
          
        <script>
            var down = document.getElementById('geeks');
      
            function GFG_Fun() {
                down.innerHTML = location.pathname.substring
                (location.pathname.lastIndexOf("/") + 1);
            }
        </script>
    </body>
      
    </html>
  • Output:



My Personal Notes arrow_drop_up
Recommended Articles
Page :