Convert URL parameters to a JavaScript Object

Given an URL with parameters in it, The task is to get those parameters and convert them to an JavaScript Object using javascript. we’re going to discuss few techniques.
First this property to know.

  • replace() method
    This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value.
    Syntax:

    string.replace(searchVal, newvalue)
    

    Parameters:

    • searchVal: This parameter is required. It specifies the value, or regular expression, that is going to replace by the new value.
    • newvalue: This parameter is required. It specifies the value to replace the search value with.

    Return value:
    Returns a new string where the defines value(s) has been replaced by the new value.

  • split() method
    This method is used to split a string into an array of substrings, and returns the new array.
    Syntax:

    string.split(separator, limit)
    

    Parameters:

    • separator: This parameter is optional. It specifies the character, or the regular expression, to use for splitting the string. If not used, the whole string will be returned (an array with only one item)
    • limit: This parameter is optional. It specifies the integer that specifies the number of splits, items beyond the split limit will be excluded from the array.

    Return value:
    Returns a new Array, having the splitted items.

    • Example 1: This example first takes the parameter portion of the URL and then using the replace() method to form an object.

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <!DOCTYPE html>  
      <html>
        
      <head>
          <title>
              JavaScript | Convert URL parameters to a Object.
          </title>
      </head>
        
      <body style="text-align:center;" id="body">
          <h1 style="color:green;"
                  GeeksForGeeks 
              </h1>
          <p id="GFG_UP" style="font-size: 19px; font-weight: bold;">
          </p>
          <button onClick="GFG_Fun()">
              click here
          </button>
          <p id="GFG_DOWN" style="color: green; font-size: 24px; font-weight: bold;">
          </p>
          <script>
              var up = document.getElementById('GFG_UP');
              var down = document.getElementById('GFG_DOWN');
              up.innerHTML = search;
              search = search.split('?')[1];
        
              function GFG_Fun() {
                  down.innerHTML = '{"' + decodeURI(search)
                  .replace(/"/g, '\\"').replace(/&/g, '","')
                  .replace(/=/g, '":"') + '"}';
              }
          </script>
      </body>
        
      </html>

      chevron_right

      
      

      Output:

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

      Example 2: This example implements the same functionality but by a different approach. It first takes the parameter portion of the URL and then using the replace() method to form an object.

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <!DOCTYPE HTML>  
      <html>
        
      <head>
          <title>
              JavaScript | Convert URL parameters to a Object.
          </title>
      </head>
        
      <body style="text-align:center;" id="body">
          <h1 style="color:green;">  
                  GeeksForGeeks  
              </h1>
          <p id="GFG_UP" style="font-size: 19px; font-weight: bold;">
          </p>
          <button onClick="GFG_Fun()">
              click here
          </button>
          <p id="GFG_DOWN" 
             style="color: green; font-size: 24px; font-weight: bold;">
          </p>
          <script>
              var up = document.getElementById('GFG_UP');
              var down = document.getElementById('GFG_DOWN');
              up.innerHTML = search;
              search = search.split('?')[1];
        
              function GFG_Fun() {
                  down.innerHTML = '{"' + search.replace(/&/g, '", "')
                                   .replace(/=/g, '":"') + '"}',
                      function(key, value) {
                          return key === "" ? value : decodeURIComponent(value)
                      };
              }
          </script>
      </body>
        
      </html>

      chevron_right

      
      

      Output:

      • Before clicking the button:
      • After clicking 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.