gpt4 book ai didi

javascript - 离开网页时发出警报

转载 作者:IT王子 更新时间:2023-10-29 02:58:28 26 4
gpt4 key购买 nike

当我尝试关闭带有未保存更改的 Google 文档选项卡时,这就是我在浏览器 (FF 3.5) 中得到的结果。

Are you sure you want to navigate away from this page?

You have unsaved changes in this document. Click Cancel now, then 'Save' to save them. Click OK now to discard them.

Press OK to continue, or Cancel to stay on the current page.

我的问题是此类警报是网络应用程序的一部分(例如 gdocs)还是由浏览器发出?如果是后者,这是怎么做到的?

最佳答案

通过浏览器。返回对话框自定义文本的是 beforeunload 事件处理程序,这只是三段的中间 - 其他两段以及按钮的文本无法自定义或以其他方式更改。

window.onbeforeunload = function(){ return 'Testing...' }

// OR

var unloadListener = function(){ return 'Testing...' };
window.addEventListener('beforeunload', unloadListener);

将产生一个对话框说

Are you sure you want to navigate away from this page?

Testing...

Press OK to continue, or Cancel to stay on the current page.

您可以通过将处理程序设置为 null 来取消它

window.onbeforeunload = null;

// OR

window.removeEventListener('beforeunload', unloadListener);

关于javascript - 离开网页时发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1289234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com