实用方法
删除hash地址参数
const deleteURlParams = (key) => {
const url = new URL(window.location.href);
// 删除哈希参数中
const hash = url.hash;
if (hash) {
const [path, queryString] = hash.slice(1).split('?');
const hashParams = new URLSearchParams(queryString);
hashParams.delete(key);
url.hash = hashParams.toString() ? `${path}?${hashParams.toString()}` : path;
}
// 更新浏览器的地址栏,不刷新页面
window.history.replaceState({}, '', url.toString());
};
删除查询地址参数
const deleteURlParams = (key) => {
const url = new URL(window.location.href);
// 删除查询参数中的
const searchParams = url.searchParams;
searchParams.delete(key);
url.search = searchParams.toString();
// 更新浏览器的地址栏,不刷新页面
window.history.replaceState({}, '', url.toString());
};
关闭页面提示
关闭系统时候二次确认
- 核心方法
window.onbeforeunload = function (e) {
e = e || window.event;
// 兼容IE8和Firefox 4之前的版本
if (e) {
e.returnValue = '关闭提示';
}
// Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
return '关闭提示';
};
- vue使用
watch(
() => route.fullPath,
(path) => {
const ignorePath = ['/login'];
// 登录页不需要提示
if (ignorePath.includes(path)) {
window.onbeforeunload = null;
} else {
window.onbeforeunload = function (e) {
e = e || window.event;
// 兼容IE8和Firefox 4之前的版本
if (e) {
e.returnValue = '关闭提示';
}
// Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
return '关闭提示';
};
}
},
{
immediate: true,
},
);