标签:
(function( window ){
var pageUtils = {
//TODO 获取浏览器URL当前请求出参数
//Key 当前需要取参数的Key,不传返回所有参数对象
getRequest : function( key ){
var paramUrl = decodeURIComponent(location.search.substr(1) );
if(!paramUrl)return null;
var paramObj = {};
//通过正则表达式替换为标准JSON字符串
var replaceUrl = paramUrl.replace(/=/g,‘":"‘).replace(/&/g,‘","‘);
replaceUrl = ‘{"‘ + replaceUrl + ‘"}‘;
try{
paramObj = JSON.parse( replaceUrl );
}catch(e){
//JSON转换失败通过传统方式取值
var paramArray = paramUrl.split("&");
for(var i = 0 ; i < paramArray.length ; i++){
var paramItem = paramArray[i];
if( paramItem.indexOf("=") > 0){
paramObj[paramItem.split("=")[0]] = paramItem.split("=")[1];
}
}
}
if( key ) return paramObj[key];
return paramObj;
}
};
window.pageUtils = pageUtils;
})( window );
标签:
原文地址:http://www.cnblogs.com/Ming2020/p/4941310.html