javascript 如何在javascript中访问请求查询字符串参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18022683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access request query string parameters in javascript?
提问by Kehlan Krumme
I've seen numerous solutions that utilize RegEx, and to be quite frank, that seems ridiculously excessive since javascript is so versatile.
我见过许多使用 RegEx 的解决方案,坦率地说,这似乎太过分了,因为 javascript 是如此多才多艺。
There must be a simpler way to access request parameters.
必须有一种更简单的方法来访问请求参数。
Could somebody demonstrate for me?
有人可以为我演示吗?
回答by Kehlan Krumme
I found a useful method in the depths of the net.
我在网的深处找到了一个有用的方法。
function querySt(Key) {
var url = window.location.href;
KeysValues = url.split(/[\?&]+/);
for (i = 0; i < KeysValues.length; i++) {
KeyValue = KeysValues[i].split("=");
if (KeyValue[0] == Key) {
return KeyValue[1];
}
}
}
Downvotes and plugins aside, thanks anyways.
撇开不赞成票和插件,还是谢谢。
回答by Adil
You can use jQuery plugin Purl (A JavaScript URL parser) v2.3.1
你可以使用 jQuery 插件 Purl (A JavaScript URL parser) v2.3.1
var queryStringParameter = $.url().param('queryStringParameter');
回答by Mike Ramsey
jQuery BBQ plugin to the rescue. See deparam method
jQuery BBQ 插件来拯救你。参见deparam 方法