js为浏览器URL追加参数

setTimeout(()=>{
    let newurl = updateQueryStringParameter(window.location.href, 'id', '123456');
    //向当前url添加参数,没有历史记录
    window.history.replaceState({
        path: newurl
    }, '', newurl);
},1000)

function updateQueryStringParameter(uri, key, value) {
    if(!value) { return uri }
    var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
    var separator = uri.indexOf('?') !== -1 ? "&" : "?";
    if (uri.match(re)) {
        return uri.replace(re, '$1' + key + "=" + value + '$2');
    }
    else {
        return uri + separator + key + "=" + value;
    }
}

文章来源:https://blog.csdn.net/wang704987562/article/details/84631740

原文地址:https://www.cnblogs.com/chensv/p/15657168.html