UNPKG

z-util-page

Version:
43 lines (42 loc) 1.05 kB
/** * 解析URL * @category 辅助函数 * @example * ```ts * const url = 'https://www.baidu.com/s?wd=hello#world' * const result = parseUrl(url) * ``` * @param url 统一资源定位符 */ export function parseUrl(url) { let Url = null; const param = {}; try { const temp = new URL(url); Url = { hash: temp.hash, host: temp.host, hostname: temp.hostname, href: temp.href, origin: temp.origin, password: temp.password, pathname: temp.pathname, port: temp.port, protocol: temp.protocol, search: temp.search, username: temp.username, searchParams: param }; } catch (error) { console.log(error); } if (Url === null) return null; const search = Url.search.slice(1); const paramList = search.split('&').map(item => item.split('=')); paramList.forEach(item => { param[item[0]] = item[1]; }); return Url; }