导航离开网页时的警报 [英] Alerts when navigating away from a web page

查看:41
本文介绍了导航离开网页时的警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试关闭包含未保存更改的 Google 文档选项卡时,这就是我在浏览器 (FF 3.5) 中看到的内容.

When I try to close my Google docs tab with unsaved changes, this is what I get in my browser (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)还是由浏览器发出?如果是后者,这是如何做到的?

My question is whether such alerts are part of the web app (gdocs for eg.) or are they given out by the browser? If latter, how is this done?

推荐答案

通过浏览器.beforeunload 事件处理程序返回对话框的自定义文本,它只是三个段落的中间部分 - 其他两个段落以及按钮的文本不能自定义或以其他方式更改.

By the browser. It's the beforeunload event handler that returns the customized text of the dialog, which is only the middle of the three paragraphs - the other two paragraphs as well as the text of the buttons cannot be customized or otherwise changed.

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

// OR

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

将产生一个对话框,上面写着

Will yield a dialog that says

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 来取消此操作

You can nullify this by setting the handler to null

window.onbeforeunload = null;

// OR

window.removeEventListener('beforeunload', unloadListener);

这篇关于导航离开网页时的警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆