const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get(‘myParam’);
原版的
为此,您不需要jQuery。您可以只使用一些纯Javascript:
function getParameterByName(name, url) { if (!url) url = window.location.href; name = name.replace(/[[]]/g, '\$&'); var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return depreURIComponent(results[2].replace(/+/g, ' '));}用法:
// query string: ?foo=lorem&bar=&bazvar foo = getParameterByName('foo'); // "lorem"var bar = getParameterByName('bar'); // "" (present with empty value)var baz = getParameterByName('baz'); // "" (present with no value)var qux = getParameterByName('qux'); // null (absent)注意:如果一个参数多次出现(
?foo=lorem&foo=ipsum),您将获得第一个值(
lorem)。对此没有标准,用法也有所不同



