首页 > 技术文章 > getUrlParam获取url参数

caiCheryl 2016-07-13 13:53 原文

function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

  

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

 var getParam = function (name) {
        var search = document.location.search;
        //alert(search);
        var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
        var matcher = pattern.exec(search);
        var items = null;
        if (null != matcher) {
            try {
                items = decodeURIComponent(decodeURIComponent(matcher[1]));
            } catch (e) {
                try {
                    items = decodeURIComponent(matcher[1]);
                } catch (e) {
                    items = matcher[1];
                }
            }
        }
        return items;
    };


way2

getRequestParams: function () {
  var url = window.location.href;
  var theRequest = {};
  if (url.indexOf("?") > -1) {
    var currentPaths = url.split("?");
    url = _.first(currentPaths);
    var paramData_str = currentPaths[1];
    var paramData_strs = paramData_str.split("&");
    _.forEach(paramData_strs, function (n) {
      _.set(theRequest, n.split("=")[0], decodeURI(n.split("=")[1]));
    });
  }
  return theRequest;
},
var regist = tools.getRequestParams().regist;

 

 

 

推荐阅读