How to add a parameter to the URL in JavaScript ?

Given a URL and the task is to add an additional parameter (name & value) to the URL using JavaScript.

URL.searchParams: This readonly property of the URL interface returns a URLSearchParams object providing access to the GET decoded query arguments in the URL.

Syntax:



var URLSearchParams = URL.searchParams;

Example 1: This example adds the parameter by using append method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
    <head
        <title
            How to add a parameter to the URL
        </title>     
    </head
      
    <body style = "text-align:center;">
           
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style =
            "font-size: 15px; font-weight: bold;">
        </p>
          
        <button onclick = "GFG_Fun()">
            Add Parameter
        </button>
          
        <p id = "GFG_DOWN" style =
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var up = document.getElementById('GFG_UP');
            var url = new URL("https://www.geeksforgeeks.org");
            up.innerHTML = url;
            var down = document.getElementById('GFG_DOWN');
              
            function GFG_Fun() {
                url.searchParams.append('param_1', 'val_1');
                down.innerHTML = url;
            }
        </script
    </body
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example 2: This example adds the parameter by using set method.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
    <head
        <title
            How to add a parameter to the URL
        </title>     
    </head
      
    <body style = "text-align:center;">
           
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style
            "font-size: 15px; font-weight: bold;">
        </p>
          
        <button onclick = "GFG_Fun()">
            Add Parameter
        </button>
          
        <p id = "GFG_DOWN" style =
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
          
        <script>
            var up = document.getElementById('GFG_UP');
            var url = new URL("https://www.geeksforgeeks.org");
            up.innerHTML = url;
            var down = document.getElementById('GFG_DOWN');
              
            function GFG_Fun() {
                url.searchParams.set('param_1', 'val_1');
                down.innerHTML = url;
            }
        </script
    </body
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.