Advertisement
riseandburn

get all URL search parameters

Feb 16th, 2024 (edited)
1,477
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var getAllUrlParams = function(url) {
  2.  
  3.     // get query string from url (optional) or window
  4.     var queryString = url ? url.split('?')[1] : window.location.search.slice(1);
  5.  
  6.     // we'll store the parameters here
  7.     var obj = {};
  8.  
  9.     // if query string exists
  10.     if (queryString) {
  11.  
  12.         // stuff after # is not part of query string, so get rid of it
  13.         queryString = queryString.split('#')[0];
  14.  
  15.         // split our query string into its component parts
  16.         var arr = queryString.split('&');
  17.  
  18.         for (var i = 0; i < arr.length; i++) {
  19.             // separate the keys and the values
  20.             var a = arr[i].split('=');
  21.  
  22.             // set parameter name and value (use 'true' if empty)
  23.             var paramName = a[0];
  24.             var paramValue = typeof (a[1]) === 'undefined' ? true : a[1];
  25.  
  26.             // (optional) keep case consistent
  27.             paramName = paramName.toLowerCase();
  28.             if (typeof paramValue === 'string') paramValue = paramValue.toLowerCase();
  29.  
  30.             // if the paramName ends with square brackets, e.g. colors[] or colors[2]
  31.             if (paramName.match(/\[(\d+)?\]$/)) {
  32.  
  33.                 // create key if it doesn't exist
  34.                 var key = paramName.replace(/\[(\d+)?\]/, '');
  35.                 if (!obj[key]) obj[key] = [];
  36.  
  37.                 // if it's an indexed array e.g. colors[2]
  38.                 if (paramName.match(/\[\d+\]$/)) {
  39.                     // get the index value and add the entry at the appropriate position
  40.                     var index = /\[(\d+)\]/.exec(paramName)[1];
  41.                     obj[key][index] = paramValue;
  42.                 } else {
  43.                     // otherwise add the value to the end of the array
  44.                     obj[key].push(paramValue);
  45.                 }
  46.             } else {
  47.                 // we're dealing with a string
  48.                 if (!obj[paramName]) {
  49.                     // if it doesn't exist, create property
  50.                     obj[paramName] = paramValue;
  51.                 } else if (obj[paramName] && typeof obj[paramName] === 'string') {
  52.                     // if property does exist and it's a string, convert it to an array
  53.                     obj[paramName] = [obj[paramName]];
  54.                     obj[paramName].push(paramValue);
  55.                 } else {
  56.                     // otherwise add the property
  57.                     obj[paramName].push(paramValue);
  58.                 }
  59.             }
  60.         }
  61.     }
  62.  
  63.     return obj;
  64. }
Tags: routing
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement