乐趣区

携带参数隐藏必要参数,瞬间改变浏览器地址

点击详情跳转的时候,有时候有必要隐藏地址栏的必要参数,本次实验是通过 sessionStorage 存储 定时器刷新浏览器方式来实现的 1. 获取 URL 地址栏参数 及参数值
function GetUrlParam(paraName) {
var url = document.location.toString();
var arrObj = url.split(“?”);

if (arrObj.length > 1) {
var arrPara = arrObj[1].split(“&”);
var arr;

for (var i = 0; i < arrPara.length; i++) {
arr = arrPara[i].split(“=”);

if (arr != null && arr[0] == paraName) {
return arr[1];
}
}
return null;
} else {
return null;
}
}
2. 将必要参数缓存到 sessionStorage 中
if (GetUrlParam(‘validKey’) != null ) {
sessionStorage.setItem(“validKey”, decodeURIComponent(GetUrlParam(‘validKey’)))

}

3. 重定向带有参数的地址
let url = location.href;
if (url.indexOf(“?”) != -1) {
url = url.split(“?”)[0];
location.href = url;
}
// 通过定时器方式刷新浏览器一次
let w1 = setTimeout(() => {
location.reload();
}, 100);
setInterval(() => {
clearTimeout(w1);
}, 100);

退出移动版