将url的查询参数解析成字典对象
方法一:
function getQueryObject(url) {
url = url == null ? window.location.href : url;
var search = url.substring(url.lastIndexOf("?") + 1);
var obj = {};
var reg = /([^?&=]+)=([^?&=]*)/g;
search.replace(reg, function (rs, $1, $2) {
var name = decodeURIComponent($1);
var val = decodeURIComponent($2);
val = String(val);
obj[name] = val;
return rs;
});
return obj;
}
getQueryObject("http://www.w3school.com.cn/tiy/t.asp?f=js_library_jquery");
//Object {f: "js_library_jquery"}
方法二:
function GetRequest(url) {
url = url == null ? window.location.href : url;//获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
GetRequest("http://www.w3school.com.cn/tiy/t.asp?f=js_library_jquery");
//Object {ttp://www.w3school.com.cn/tiy/t.asp?f: "js_library_jquery"}