1. 程式人生 > >JS 獲取href帶引數跳轉的值

JS 獲取href帶引數跳轉的值

//var i = 1;
//var j = 2;
//用法(1) local.href = "xxx.html?id=1"
//用法(2) local.href = "xxx.html?id=" + i
//用法(3) local.href = "xxx.html?id=" + i "&j=" j

function getUrlParam(name) {
    //構造一個含有目標引數的正則表示式物件
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); 
    var r = window.location.search.substr(1).match(reg); //匹配目標引數
    if (r != null) {
        return unescape(r[2]);
    } else {
        return null; //返回引數值
    }
}
//獲取引數
getUrlParam(id)
getUrlParam(j)