Javascript 读取 url JQuery 中的 GET 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4545697/
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
read the GET variables in url JQuery
提问by Russell Parrott
Sorry for another "simple" question, but is there an easy way to read the GET variables from a URL. example. I have a url http://www.domain.com/page.php?var1=1
In my case I will only have 1 variable i.e. var1
or var2
(the variable can change but there will only every be one per url). All the tuts I have seen relate to arrays rather than "singletons" OK I know an array solution may be better but this is just a simple single get variable. Any suggestions? Thanks in advance
抱歉另一个“简单”的问题,但是有没有一种简单的方法可以从 URL 读取 GET 变量。例子。我有一个 urlhttp://www.domain.com/page.php?var1=1
在我的情况下,我将只有 1 个变量,即var1
或var2
(变量可以更改,但每个 url 只有一个)。我所看到的所有 tuts 都与数组有关,而不是“单例” 好吧,我知道数组解决方案可能更好,但这只是一个简单的单个 get 变量。有什么建议?提前致谢
回答by usoban
var split = location.search.replace('?', '').split('=')
split[0]
is your var name, and split[1]
is your var value. You actually don't really need jQuery for that piece of code ;)
split[0]
是您的 var 名称,split[1]
是您的 var 值。对于那段代码,您实际上并不真正需要 jQuery ;)
As for twiz's comment, splitting multiple variables can be done like that:
至于 twiz 的评论,可以这样拆分多个变量:
var split = location.search.replace('?', '').split('&').map(function(val){
return val.split('=');
});
You can access variable name by split[index][0]
and value by split[index][1]
.
您可以通过访问变量名称split[index][0]
和值split[index][1]
。
Of course you can use the second snippet instead of the first one for one variable too.
当然,您也可以对一个变量使用第二个片段而不是第一个片段。
回答by Tiele Declercq
I use this in my default javascript file.
我在我的默认 javascript 文件中使用它。
var get = [];
location.search.replace('?', '').split('&').forEach(function (val) {
split = val.split("=", 2);
get[split[0]] = split[1];
});
Now you can use them by name:
现在您可以按名称使用它们:
get["var1"]
回答by ungalcrys
You can use this function that returns the value of the specified var name from url if the var exists. Otherwise you will get an empty string.
如果 var 存在,您可以使用此函数从 url 返回指定 var 名称的值。否则你会得到一个空字符串。
function getUrlValue(varName) {
var split = $(location).attr('href').split('?');
var value = '';
if (split.length == 2) {
split = split[1].split('&');
for (var i = 0; i < split.length; i+=1) {
var keyValue = split[i].split('=');
if (keyValue.length == 2 && keyValue[0] == varName) {
value = keyValue[1];
break;
}
}
}
return value;
}
回答by jla
With ES2019 to create an object:
用 ES2019 创建对象:
let GET = Object.fromEntries( location.search
.replace( '?', '' )
.split( '&' )
.map( i => i.split( '=' ) ) );
The Object.fromEntriesmethod creates an object from an array.
该Object.fromEntries方法创建从阵列的对象。
For the URL http://www.example.com/page?var1=1&var2=2
the above code returns the object
对于 URL http://www.example.com/page?var1=1&var2=2
,上面的代码返回对象
{
var1: '1',
var2: '2'
}