获取地址栏参数并重定向

207 阅读1分钟
// 例如当前URL:http://xxxx.com/?token=xxxxx&productNo=0g00#/user

const url = new URL(window.location.href);
const params = new URLSearchParams(url.search);
const token = params.get('token');
const productNo = params.get('productNo');

if (token) {
    sessionStorage.setItem('token', token);
    sessionStorage.setItem('productNo', productNo);
    setTimeout(function () {  //重定向,隐藏地址栏参数
        let url = new URL(window.location.href);
        url.searchParams.delete('token');
        url.searchParams.delete('productNo');
        let new_url = url.toString();
        history.replaceState('userManage', '', new_url); //修改网址
 });}