通过截取 URL 地址获取参数:

function getUrlParam(name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substring(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
}

调用:

//URL: https://www.baidu.com/s?wd=Abc&cid=111
let wd = getUrlParam('wd');
console.log(wd);   //Abc
let cid = getUrlParam('cid');
console.log(cid);  //111
Last modification:January 15, 2023
如果觉得我的文章对你有用,请随意赞赏