/**
* 获取地址栏参数值
* @param {string } name 参数名
* @returns
*/
function getQueryString(name) {
const url = location.href
const urlSearchParams = new URLSearchParams(url)
const params = Object.fromEntries(urlSearchParams.entries())
return params[name]
}
/**
* 获取地址栏参数值
* @param {String} name 参数名
* @param {Boolean} startLastIndexOf 是否从最后一个问号?的参数获取url参数,这是因为url可能会有多个问号?
* @returns
*/
function getUrlParam(name, startLastIndexOf = false) {
let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)') // 构造一个含有目标参数的正则表达式对象
// let r = window.location.search.substr(1).match(reg) // 匹配目标参数
let splitsArray = window.location.href.split('?')
const index = splitsArray.length > 1 && startLastIndexOf ? splitsArray.length - 1 : 1
let search = splitsArray.length > 1 ? splitsArray[index] : ''
for (let i = 2; i < splitsArray.length; i++) {
search += `?${splitsArray[i]}`
}
let r = search.match(reg)
if (r != null) return decodeURIComponent(r[2])
return null // 返回参数值
}
/**
* 获取encode后的url参数
* @param {String} name 参数名
* @returns
*/
function getEncodeUrlParam(name){
if (!name) {
return ''
}
const keyStart = location.href.lastIndexOf('?')
const params = location.href.slice(keyStart + 1)
let arr = decodeURIComponent(params).split('&')
let res = ''
arr.forEach((item, index) => {
if (item.indexOf(name + '=') > -1) {
res = arr[index]
}
})
// console.log('arr', arr, res)
const i = res.indexOf(name + '=') + name.length
return res.slice(i+1)
}