获取URL地址栏参数(正则表达式)

/**
* 获取地址栏参数
* @method queryURLParameter
* @param {url:不传默认是当前locationhref ,isEncoded:是否是加密,不加密false,加密需要解密:true}
* @return {参数对象}
* eg:https://i.cnblogs.com/EditPosts.aspx?opt=1&item=youth
* queryURLParameter('https://i.cnblogs.com/EditPosts.aspx?opt=1&item=youth',false)=>{opt:1,item:youth}
*/
function queryURLParameter(url, isEncode) {
  url = url || window.location.href;
  var obj = {}, reg = /([^?&=]+)=([^?&=]+)/g;
  url.replace(reg, function () {
  var arg = arguments;
  obj[arg[1]] = isEncode ? decodeURIComponent(arg[2]) : arg[2];
  });
  return obj;
}

posted on 2017-04-13 10:33  myYouth  阅读(835)  评论(0编辑  收藏  举报

导航