我试图使用javascript获取网址值,到目前为止,我只能得到纯数字,而不是字母或字母的混合数字.我找不到任何一个函数的工作示例,它允许检索带有字母的数字,只是数字.我没有使用任何非字母数字字符.我想要传递的示例值是"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"];
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒.谢谢.
以下是上述两个答案的简要版本:
function gup (name) {
name = RegExp ('[?&]' + name.replace (/([[\]])/, '\\$1') + '=([^&#]*)');
return (window.location.href.match (name) || ['', ''])[1];
}
Run Code Online (Sandbox Code Playgroud)
更容易打字,更容易处理,恕我直言更容易阅读.因人而异