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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM