A technique you can use to get all query string parameters and push them into an object of key/value pairs.
/**
* Get the URL parameters
* source: https://css-tricks.com/snippets/javascript/get-url-variables/
* @param {String} url The URL
* @return {Object} The URL parameters
*/
var getParams = function (url) {
var params = {};
var parser = document.createElement('a');
parser.href = url;
var query = parser.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
params[pair[0]] = decodeURIComponent(pair[1]);
}
return params;
};
Use it like this:
// Get parameters from the current URL
getParams(window.location.href);
// Get parameters from a URL string
var url = 'https://gomakethings.com?sandwhich=chicken%20salad&bread=wheat';
getParams(url);
Source
https://vanillajstoolkit.com/helpers/getparams/
https://css-tricks.com/snippets/javascript/get-url-variables/