From 6c73978c09351055acc0a0a921776203941ab492 Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Sat, 16 Sep 2023 14:00:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20src/utils/utils.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/utils.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 4a1b19e4..16ba9431 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -330,3 +330,23 @@ export const JSONParse = (data: string) => { export const setTitle = (title?: string) => { title && (document.title = title) } + +/** + * 处理网页关闭事件 + */ +export const addWindowUnload = () => { + // 关闭网页出现离开提示 + window.onbeforeunload = function (e) { + e = e || window.event + // 兼容IE8和Firefox 4之前的版本 + if (e) { + e.returnValue = '您确定要离开当前页面吗?请确认是否保存数据!' + } + // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+ + return '您确定要离开当前页面吗?请确认是否保存数据!' + } + // 返回销毁事件函数 + return () => { + window.onbeforeunload = null + } +} \ No newline at end of file