1const queryString = window.location.search;
2const parameters = new URLSearchParams(queryString);
3const value = parameters.get('key');
1const urlParams = new URLSearchParams(window.location.search);
2const myParam = urlParams.get('myParam');
3
1function getParameterByName(name, url = window.location.href) {
2 name = name.replace(/[\[\]]/g, '\\$&');
3 var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
4 results = regex.exec(url);
5 if (!results) return null;
6 if (!results[2]) return '';
7 return decodeURIComponent(results[2].replace(/\+/g, ' '));
8}
9
10// query string: ?foo=lorem&bar=&baz
11var foo = getParameterByName('foo'); // "lorem"
12var bar = getParameterByName('bar'); // "" (present with empty value)
13var baz = getParameterByName('baz'); // "" (present with no value)
14var qux = getParameterByName('qux'); // null (absent)