Saturday, October 25, 2025
HomeLanguagesJavascriptHow to get the file name from page URL using JavaScript ?

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

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.

  • 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;
            }
            #neveropen {
                font-size: 26px; 
                font-weight: bold; 
                color: green;
            }
        </style>
    </head>
      
    <body>
        <h1>neveropen</h1>
          
        <p>
            Click on the button to get
            the name of the file.
        </p>
          
        <button onclick="GFG_Fun();">
            click here
        </button>
          
        <p id="neveropen"></p>
          
        <script>
            var down = document.getElementById('neveropen');
      
            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;
            }
            #neveropen {
                font-size: 26px; 
                font-weight: bold; 
                color: green;
            }
        </style>
    </head>
      
    <body>
        <h1>neveropen</h1>
          
        <p>
            Click on the button to get
            the name of the file.
        </p>
          
        <button onclick="GFG_Fun();">
            click here
        </button>
          
        <p id="neveropen"></p>
          
        <script>
            var down = document.getElementById('neveropen');
      
            function GFG_Fun() {
                down.innerHTML = location.pathname.substring
                (location.pathname.lastIndexOf("/") + 1);
            }
        </script>
    </body>
      
    </html>

    
    
  • Output:
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS