一、获取url上单独的参数
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return decodeURI(r[2]);//解码
}
return null;
}
使用方法so easy啦:
比如,你的url是这样的
http://localhost:8999/html/user/find-result.html?department=2&technical=1&time=d&area=dd单独
直接调用:
console.log(GetQueryString("department"));
输出:2
二、获取url的对象
function getUrlData() {
var purl = window.location.href;
purl = purl.substr(purl.indexOf("?") + 1);
var urlData = JSON.parse(decodeURI(purl.substr(purl.indexOf("=") + 1)));
return urlData;
}
如果url后面跟着的是一个对象
http://localhost:8999/html/user/find-result.html?data=%7B%22groupid%22:%226ff83afa43a7472b85d4751f21668b17%22,%22status%22:%221%22,%22source%22:1%7D
调用该方法:getUrlFn()
返回是一个data对象
这样也可以获取任意一个元素,var groupid = data.groupid...