/**
* 获取 url 参数
*
* @param {string} name name
* @param {string} urls urls
* @returns {*} value
*/
export const getUrlParam = (name, urls) => {
const { hash, search } = window.location;
const url = urls || hash || search;
const index = url.indexOf('?');
const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
const result = decodeURIComponent(url)
.substr(index + 1)
.match(reg);
return result === null ? result : result[2];
};
原生方法URLSearchParams
URLSearchParams
对象专门用于处理url网址信息中的查询字符串,在网址字符串中通常都是 ? 问号之后的内容(不包含问号)。
在URL对象中也有一个属性 searchParams
返回该对象,会对查询字符串做解析处理,以等号分割生成对应的 key-value 键值对的数据形式。
const sq = window.location.search
const urlSearchParams = new URLSearchParams(sq)
urlSearchParams.get('ticket')