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. -
Before clicking the button:
-
After clicking the button:
-
Before clicking the button:
-
After clicking the button:
- How to serialize an object into a list of URL query parameters using JavaScript ?
- How to convert an object to string using JavaScript?
- Convert an Array to an Object in JavaScript
- JavaScript | Know the value of GET parameters from URL
- JavaScript | Function Parameters
- How to declare the optional function parameters in JavaScript ?
- JavaScript | Passing parameters to a callback function
- How to check a JavaScript Object is a DOM Object ?
- Object.is( ) in JavaScript
- JavaScript | History object
- JavaScript | ArrayBuffer Object
- Object.isSealed( ) In JavaScript
- Object.keys( ) In JavaScript
- Object.seal( ) In JavaScript
- How to add key/value pair to a JavaScript object?
Example 1: This example first takes the parameter portion of the URL and then using the replace() method to form an object.
<!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'); var search = 'https://www.geeksforgeeks.org/?param_1=val_1&x;=7&y;=9'; up.innerHTML = search; search = search.split('?')[1]; function GFG_Fun() { down.innerHTML = '{"' + decodeURI(search) .replace(/"/g, '\\"').replace(/&/g, '","') .replace(/=/g, '":"') + '"}'; } </script> </body> </html> |
Output:
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.
<!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'); var search = 'https://www.geeksforgeeks.org/?param_1=val_1&x;=7&y;=9'; 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> |
Output:
Recommended Posts:
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.



