JavaScript | Know the value of GET parameters from URL

In order to know the parameters, those are passed by the “GET” method, like sometimes we pass the Email-id, Password, and other details. For that purpose, We are using the following snippet of code.

Example-1: This example get the value of “a” parameter from the URL.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript |
      Know the value from GET parameters
    </title>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
  
    <button onclick="myGeeks()">
        Click here
    </button>
  
    <p id="GFG_down"
       style="color:green;">
    </p>
  
    <script>
        function myGeeks() {
            var GFG_url_string = 
              
            var GFG_url = new URL(GFG_url_string);
            var c = GFG_url.searchParams.get("a");
            var p_down = document.getElementById("GFG_down");
            p_down.innerHTML = c;
        }
    </script>
</body>
  
</html>

chevron_right


Output:



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

Example:This example get the value of all the parameters from the URL.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript 
      | Know the value from GET parameters
    </title>
</head>
  
<body style="text-align:center;">
  
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
  
    <button onclick="myGeeks()">
        Click here
    </button>
  
    <p id="GFG_down"
       style="color:green;">
    </p>
  
    <script>
        function myGeeks() {
            var GFG_url_string = 
            var GFG_url = new URL(GFG_url_string);
            var a = GFG_url.searchParams.get("a");
            var b = GFG_url.searchParams.get("b");
            var c = GFG_url.searchParams.get("c");
            var p_down = document.getElementById("GFG_down");
            p_down.innerHTML = a + "<br>" + b + "<br>" + c;
        }
    </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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.