js原生获取地址参数和设置删除

分类:前端来源:站内 最近更新:2020-09-10 20:27:28浏览:1072留言:0

/* 获得地址参数 */
/* ================================================== */
getParam = function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
};
/* 设置地址参数 */
/* ================================================== */
setParam = function(name, value) {
    var currentUrl = window.location.href.split('#')[0];
    if (/\?/g.test(currentUrl)) {
        if (/name=[-\w]{4,25}/g.test(currentUrl)) {
            currentUrl = currentUrl.replace(/name=[-\w]{4,25}/g, name + "=" + value);
        } else {
            currentUrl += "&" + name + "=" + value;
        }
    } else {
        currentUrl += "?" + name + "=" + value;
    }
    if (window.location.href.split('#')[1]) {
        return currentUrl + '#' + window.location.href.split('#')[1];
    } else {
        return currentUrl;
    }
};


1

发表评论

评论列表(0)

  • 暂时没有留言
热门