获取当前的url并移除不想要的字段

function paramsFormat(url) {
    var qInd = url.indexOf('?');
    var sharpInd = url.indexOf('#'); //路由
    var search = "";
    var paramsList = [];
    var paramsObj = {};

    if (qInd >= 0) {
        if (sharpInd > 0) {
            search = url.substring(qInd + 1, sharpInd);
        } else {
            search = url.substring(qInd + 1);
        }
        paramsList = search.split('&');
        for (var ind=0;ind<paramsList.length;ind++) {
            var param = paramsList[ind];
            if(param) {
                var pind = param.indexOf("=");
                if (pind >= 0) {
                    paramsObj[param.substring(0, pind)] = param.substr(pind + 1);
                } else {
                    paramsObj[param] = "";
                }
            }

        }
    }
    return paramsObj;
}
function getHost() {
    var url = location.protocol + "//" + location.host;
    if (location.port) {
        url += ":" + location.port;
    }
    return url;
}
function getUrl(removeKey) {
    var urlParams = paramsFormat(window.location.href);
    var url = '';
      if (urlParams[removeKey]) {
        url = getHost() + location.pathname;
        if (location.href.indexOf('&')!=-1) {
            url += '?';
        }
        for (var prop in urlParams) {
            if (prop!=removeKey) {
                url+= prop+'='+urlParams[prop]+'&'
            }
        }
      } else {
        url = location.href;
      }
    return url
}
getUrl('removeKey');
window.history.replaceState(null, "", url);

  

posted @ 2019-05-14 10:28  李晓菲  阅读(325)  评论(0编辑  收藏  举报