例如我有网址:
http ://www.website.com/example?something=abc&a=b
我怎样才能得到东西的内容,“abc”和a的内容作为字符串?
提前致谢, Mateiaru
例如我有网址:
http ://www.website.com/example?something=abc&a=b
我怎样才能得到东西的内容,“abc”和a的内容作为字符串?
提前致谢, Mateiaru
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])
});
使用函数助手:
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}
然后这样称呼它:
var aValue = getURLParameter(a);
//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"]);