1.获取地址栏参数
/**
* qs2obj('file:///E:/wamp/www/11-10-yunnan/column_xwdt.html?abc=1&aaa=2')
* qs2obj(window.location.href).abc)
* @param {String} url url地址栏
* @return {Object}
*/
function qs2obj(url) {
var qs = url.split("?")[1];
var arr = [];
var res = {};
if(!qs) {
// return res;
} else {
arr = qs.split("&");
for(var i = 0, len = arr.length; i < len; i++) {
var key = arr[i].split("=")[0];
var val = arr[i].split("=")[1];
res[key] = decodeURIComponent(val);
}
}
return res;
}
2.将对象转换为地址栏参数
/**
* obj2qs({'a':1,'b':2})
* @author alan
* @param {Object} obj 需要拼接的参数对象
* @return {String}
* */
function obj2qs(obj) {
if(!obj && !Object.keys(obj).length) {
return "";
} else {
var arr = [];
for(var key in obj) {
arr.push(key + "=" + obj[key]);
}
return arr.join("&");
}
}
3.JS携带参数跳转页面
// 跳转页面js
// goPage('single_detail.html')
// goPage('single_detail.html',{'id':12,'val':123})
function goPage(_src,_obj){
_obj?window.location.href= _src + '?'+obj2qs(_obj):window.location.href= _src;
}