Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. function getAllUrlParams(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. // in case params look like: list[]=thing1&list[]=thing2
  23. var paramNum = undefined;
  24. var paramName = a[0].replace(/\[\d*\]/, function(v) {
  25. paramNum = v.slice(1,-1);
  26. return '';
  27. });
  28.  
  29. // set parameter value (use 'true' if empty)
  30. var paramValue = typeof(a[1])==='undefined' ? true : a[1];
  31.  
  32. // (optional) keep case consistent
  33. paramName = paramName.toLowerCase();
  34. paramValue = paramValue.toLowerCase();
  35.  
  36. // if parameter name already exists
  37. if (obj[paramName]) {
  38. // convert value to array (if still string)
  39. if (typeof obj[paramName] === 'string') {
  40. obj[paramName] = [obj[paramName]];
  41. }
  42. // if no array index number specified...
  43. if (typeof paramNum === 'undefined') {
  44. // put the value on the end of the array
  45. obj[paramName].push(paramValue);
  46. }
  47. // if array index number specified...
  48. else {
  49. // put the value at that index number
  50. obj[paramName][paramNum] = paramValue;
  51. }
  52. }
  53. // if param name doesn't exist yet, set it
  54. else {
  55. obj[paramName] = paramValue;
  56. }
  57. }
  58. }
  59.  
  60. return obj;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement