重写URL

更新网页url

/***省略部分代码***/ 
function rewriteUrl(url) {
        if (url.substr(0, 1) === "/") {
            url = (rootdir || "") + url;
        }
        else if (url.substr(0, 2) === "~/") {
            url = (wwwdir || "") + url.replace(/^~/, '');
        }
        url = setQueryStringByName(url, "prjID", prjId);
        url = setQueryStringByName(url, "dptID", dptID);
        return url;
    }
/***省略部分代码***/ 

 

 

function setQueryStringByName(url, name, value) {
    var splitIndex = url.indexOf("?") + 1;
    if (splitIndex > 0) {
        var paramStr = url.substr(splitIndex, url.length);
        var newUrl = url.substr(0, splitIndex);
        // - if exist , replace
        var arr = paramStr.split('&');
        for (var i = 0; i < arr.length; i++) {
            var kv = arr[i].split('=');
            if (kv[0] == name) {
                newUrl += kv[0] + "=" + value;
            } else {
                if (kv[1] != undefined) {
                    newUrl += kv[0] + "=" + kv[1];
                }
            }
            if (i != arr.length - 1) {
                newUrl += "&";
            }
        }
    }
    else {
        newUrl = url;
    }
    if (newUrl.indexOf(name) < 0) {
        newUrl += splitIndex == 0 ? "?" + name + "=" + value : "&" + name + "=" + value;
    }
    return newUrl;
}

 

posted @ 2019-01-05 10:08  深海地平线  阅读(184)  评论(0编辑  收藏  举报