js截取url參數


舉例說明,比如http://localhost:2019/blog/getCommentListInfo?postId=1
如何獲取postId=1這個參數值呢?
很簡單通過下面代碼即可獲取,如:

window.onload = function() {

    var postId = getUrlParms("postId");

    getByIdCommentInfo(postId);
}

//獲取地址欄參數,name:參數名稱
function getUrlParms(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r != null)
        return unescape(r[2]);
    return null;
}

參考資料:
js獲取url傳遞參數,js獲取url?號后面的參數:https://www.cnblogs.com/karila/p/5991340.html


免責聲明!

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



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