乐趣区

关于javascript:获取当前页面URL参数

/**
 * 获取以后页面 URL 参数(办法一)* @param {*} name 
 */
function query(name) {const search = location.search.substr(1)
    const arr1 = search.split('&')
    for(let i in arr1) {let arr2 = arr1[i].split('=')
        if (arr2[0] === name) {return arr2[1]
        }
    }
    return null
}
/**
 * 获取以后页面 URL 参数(办法二)* @param {*} name 
 */
function query2(name) {const search = location.search.substr(1)
    const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i')
    const res = search.match(reg)
    if (res == null) {return null}
    return res[2]
}
退出移动版