1 回答
TA贡献1780条经验 获得超5个赞
function getParamName(attr) { //兼容IE9以下浏览器
var obj = {};
var sesrch = window.location.search;
var arr_search = sesrch.split('?')[1];
arr_search = arr_search.split('&');
for (var i = 0; i < arr_search.length; i++) {
var target = arr_search[i].split('=');
obj[target[0]] = target[1];
}
return obj[attr];
}
//getParamName('flag');
function getParamName(attr) { //数组forEach方法实现
var obj = {};
window.location.search.split('?')[1].split('&').forEach(function(item, index) {
obj[item.split('=')[0]] = item.split('=')[1];
});
return obj[attr];
}
//getParamName('flag');
function getParamName(attr) { //数组filter方法实现
var obj = {};
var newarr = window.location.search.split('?')[1].split('&').filter(function(item, index) {
return item.split('=')[0] == attr;
});
return newarr[0].split('=')[1];
}
//getParamName('flag');
function getParamName(name) {//正则表达式实现
var match = RegExp('[?&]' + name + '=([^&]*)')
.exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
};
//getParamName('flag');
平时自己写的几种方法,仅供参考,我推荐最后正则那个,简单粗暴
添加回答
举报