zoukankan      html  css  js  c++  java
  • 获取当前的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);

      

  • 相关阅读:
    前端页面获取各类页面尺寸及坐标尺寸总结
    禁止微信内置浏览器调整字体大小
    区分浏览器,判断浏览器版本
    JavaScript
    ASP.NET MVC,Entity Framework 及 Code First
    循序渐进MongoDB V3.4(Ubuntu)
    Webpack
    RequireJS Step by Step
    JavaScript Object 及相关操作
    ES6 Promises
  • 原文地址:https://www.cnblogs.com/floraCnblogs/p/url_cut_replaceState.html
Copyright © 2011-2022 走看看