1

例如我有网址:
http ://www.website.com/example?something=abc&a=b

我怎样才能得到东西的内容,“abc”和a的内容作为字符串?

提前致谢, Mateiaru

4

3 回答 3

2
var path = window.location.href;
var index = path.indexOf('?');
var paramstr = path.substring(index + 1);
var paramstrs = paramstr.split('&');
paramstrs.forEach(function(str, index){
    var parts = str.split('=');
    alert('value of ' + parts[0] + ' is ' + parts[1])
});
于 2013-04-20T11:33:41.667 回答
1

使用函数助手:

function getURLParameter(name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}

然后这样称呼它:

var aValue = getURLParameter(a);
于 2013-04-20T11:33:01.350 回答
1
//var url = window.location.href;
var url = "http://www.website.com/example?something=abc&a=b";
var attributes = url.split("?")[1].split("&");
var keyValue = {};
for(i in attributes)
{
    var pair = attributes[i].split("=");
    keyValue[pair[0]] = pair[1];
}

alert(keyValue["something"]);
alert(keyValue["a"]);
于 2013-04-20T11:35:17.223 回答