使用 Javascript 从 url 参数中获取变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8460265/
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
Get a variable from url parameter using Javascript
提问by cWoolf
I am trying to get a url value using javascript, so far I can only get pure numbers, not mixed numbers with letters or just letters. I can't find any working examples of a function that allows for numbers with letters to be retrieved, just numbers. I am not using any non alphanumeric characters. An example value that I am trying to pass is "42p316041610751874cm83p2306600132141".
我正在尝试使用 javascript 获取 url 值,到目前为止,我只能获取纯数字,不能将数字与字母混合或仅获取字母。我找不到任何允许检索带字母的数字的函数的工作示例,只有数字。我没有使用任何非字母数字字符。我试图传递的一个示例值是“42p316041610751874cm83p2306600132141”。
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
var first = getUrlVars()["test"];
Any help would be great. Thanks.
任何帮助都会很棒。谢谢。
采纳答案by HBP
Here is a condensed version of two of the answers above :
这是上述两个答案的精简版本:
function gup (name) {
name = RegExp ('[?&]' + name.replace (/([[\]])/, '\') + '=([^&#]*)');
return (window.location.href.match (name) || ['', ''])[1];
}
Easier to type, easier on processing and, IMHO easier to read. YMMV
更容易打字,更容易处理,恕我直言更容易阅读。青年会
回答by Saeed Neamati
I use this function, and it rocks. I don't remember from where I took it, but it's a good one:
我使用这个功能,它很摇滚。我不记得我从哪里拿的,但它是一个很好的:
function gup(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regexS = "[\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return results[1];
}
If you have a URL like http://www.exmaple.com/path?p1=lkjsd234&p2=klsjd987
, you can use:
如果你有一个像 的 URL http://www.exmaple.com/path?p1=lkjsd234&p2=klsjd987
,你可以使用:
alert(gup('p1')); // shows 'lkjsd234';
回答by Dominic Green
Your code looks like it should work I use the function below if it helps
您的代码看起来应该可以工作,如果有帮助,我会使用下面的函数
function getParam(name){
name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
var regexS = "[\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec (window.location.href);
if (results == null)
return "";
else
return results[1];
}
var first = getParam("test");
回答by FK82
Your approach should work. Here's my version for comparison:
你的方法应该有效。这是我的比较版本:
var o = {
keys: [ ],
values: [ ]
}
/*
You could just use the window#location#search value to get the query sub-String of the currently loaded URL
var q = window.location.search.substring(1) ;
For now we'll use a query String from a Google search for "MDN window location"
*/
q = "q=mdn+window+location&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:de:official&client=firefox-a"
for( var i = 0 , a = q.split("&"), p ; i < a.length ; i++ ) {
p = a[i] ;
if( ( b = p.split("=") ) != null ) {
o.keys[i] = b[0] ;
o.values[i] = b[1] ;
}
}
console.log("(!!) o: " + o.toSource( ) ) ;