如何捕捉用户离开页面并取消页面 [英] How to catch user leaving a page and cancelling it

查看:26
本文介绍了如何捕捉用户离开页面并取消页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户离开 GWT 应用程序时,我想打开一个确认对话框并为他们提供留下的选择,即 Confirm("你确定要离开此页面吗", "yes", "no").

When a user leaves the GWT app, I would like to open a confirm dialog and offer them the choice to stay, i.e. Confirm("are you sure you want to leave this page", "yes", "no").

我知道如何构建拨号框.:)

I know how to build the dialbox. :)

问题是,如何捕捉用户离开页面的事件以及如何取消它?

The question is, how to I catch the event of a user leaving the page and how to I cancel it?

丹尼尔

推荐答案

致电 Window.addWindowClosingHandler,并将其传递给调用 setMessage 在 Window.ClosingEvent 上,像这样:

Call Window.addWindowClosingHandler, and pass it a callback that calls setMessage on the Window.ClosingEvent, like so:

Window.addWindowClosingHandler(new Window.ClosingHandler() {
      public void onWindowClosing(Window.ClosingEvent closingEvent) {
        closingEvent.setMessage("Do you really want to leave the page?");
      }
    });

(我已经添加了 GWT 2.0 文档的链接;将这些 URL 中的 2.0 更改为 1.6 以查看 GWT 1.6/1.7 文档.)

(I've put in links to the GWT 2.0 docs; change the 2.0 to 1.6 in those URLs to see the GWT 1.6/1.7 docs.)

请注意,这样做,您不必/不必自己创建对话框.

Note that doing it this way, you don't have to/don't get to create the dialog box yourself.

这篇关于如何捕捉用户离开页面并取消页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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