如何在 JavaScript 中获取查询字符串值? [英] How can I get query string values in JavaScript?
问题描述
是否有无插件的方式通过 jQuery 检索查询字符串)?
如果是这样,怎么办?如果没有,有没有可以做到这一点的插件?
更新:2021 年 6 月
对于需要所有查询参数的特定情况:
const urlSearchParams = new URLSearchParams(window.location.search);const params = Object.fromEntries(urlSearchParams.entries());
更新:2018 年 9 月
您可以使用 URLSearchParams,它既简单又具有良好(但不完整)的浏览器支持.
const urlParams = new URLSearchParams(window.location.search);const myParam = urlParams.get('myParam');
原创
为此您不需要 jQuery.您可以只使用一些纯 JavaScript:
function getParameterByName(name, url = window.location.href) {name = name.replace(/[[]]/g, '\$&');var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),结果 = regex.exec(url);如果(!结果)返回空;如果 (!results[2]) 返回 '';返回 decodeURIComponent(results[2].replace(/+/g, ' '));}
用法:
//查询字符串:?foo=lorem&bar=&bazvar foo = getParameterByName('foo');//逻辑"var bar = getParameterByName('bar');//"";(存在空值)var baz = getParameterByName('baz');//"";(目前没有价值)var qux = getParameterByName('qux');//空(不存在)
注意:如果一个参数出现多次(?foo=lorem&foo=ipsum
),您将获得第一个值(lorem
).没有关于此的标准并且用法各不相同,例如参见这个问题:重复 HTTP GET 查询键的权威位置.
注意:该函数区分大小写.如果您更喜欢不区分大小写的参数名称,将 'i' 修饰符添加到 RegExp>
注意:如果你得到一个 no-useless-escape eslint 错误,你可以替换 name = name.replace(/[[]]/g, '\$&');
with name = name.replace(/[[]]/g, '\$&')
.
这是基于新的URLSearchParams 规范的更新更简洁地实现相同的结果.请参阅标题为URLSearchParams的答案"下面.
Is there a plugin-less way of retrieving query string values via jQuery (or without)?
If so, how? If not, is there a plugin which can do so?
Update: June-2021
For a specific case when you need all query params:
const urlSearchParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries());
Update: Sep-2018
You can use URLSearchParams which is simple and has decent (but not complete) browser support.
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');
Original
You don't need jQuery for that purpose. You can use just some pure JavaScript:
function getParameterByName(name, 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 decodeURIComponent(results[2].replace(/+/g, ' '));
}
Usage:
// query string: ?foo=lorem&bar=&baz
var 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)
NOTE: If a parameter is present several times (?foo=lorem&foo=ipsum
), you will get the first value (lorem
). There is no standard about this and usages vary, see for example this question: Authoritative position of duplicate HTTP GET query keys.
NOTE: The function is case-sensitive. If you prefer case-insensitive parameter name, add 'i' modifier to RegExp
NOTE: If you're getting a no-useless-escape eslint error, you can replace name = name.replace(/[[]]/g, '\$&');
with name = name.replace(/[[]]/g, '\$&')
.
This is an update based on the new URLSearchParams specs to achieve the same result more succinctly. See answer titled "URLSearchParams" below.
这篇关于如何在 JavaScript 中获取查询字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!