JS删除地址栏中指定参数

/*删除地址栏中,指定的参数名*/
    function delParam(paramKey) {
        var url = window.location.href;    //页面url
        var urlParam = window.location.search.substr(1);  //页面参数
        var beforeUrl = url.substr(0, url.indexOf("?"));  //页面主地址(参数之前地址)
        var nextUrl = "";
        var arr = new Array();
        if (urlParam != "") {
            var urlParamArr = urlParam.split("&"); //将参数按照&符分成数组
            for (var i = 0; i < urlParamArr.length; i++) {
                var paramArr = urlParamArr[i].split("="); //将参数键,值拆开
                //如果键雨要删除的不一致,则加入到参数中
                if (paramArr[0] != paramKey) {
                    arr.push(urlParamArr[i]);
                }
            }
        }
        if (arr.length > 0) {
            nextUrl = "?" + arr.join("&");
        }
        url = beforeUrl + nextUrl;
        return url;
    }

 

posted @ 2020-07-06 16:14  可达鸭给我冲鸭  阅读(4253)  评论(0编辑  收藏  举报