getParam.js
Get the value of a query string from a URL.
Example
let url = 'http://example.com?this=chicken&that=sandwich&topping=tomato&topping=spicy+mayo';
// returns "chicken"
let thisOne = getParam('this', url);
// returns "sandwich"
let thatOne = getParam('that', url);
// returns "tomato"
let topping = getParam('topping', url);
// returns "spicy mayo"
let anotherOne = getParam('another', url);
The helper function
/**
* Get the value of a query string from a URL
* (c) Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {String} param The parameter to get the value of
* @param {String} url The URL to get the value from [optional]
* @return {String} The value
*/
function getParam (param, url = window.location) {
let params = new URL(url).searchParams;
let val = params.getAll(param);
if (val.length > 1) return val;
return val[0];
}