欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-24 09:40:35
...
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);

  

转载于:https://www.cnblogs.com/floraCnblogs/p/url_cut_replaceState.html