且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何抓住用户离开一个页面并取消它

更新时间:2023-12-01 21:15:04

致电 Window.addWindowClosingHandler ,并传递一个回调,该回调调用 setMessage 在Window.ClosingEvent上,如下所示:

  Window.addWindowClosingHandler(new Window.ClosingHandler(){
public void onWindowClosing(Window.ClosingEvent closingEvent){
closingEvent.setMessage(您真的要离开页面吗?);
}
});

(我已经链接到GWT 2.0文档;在这些URL中将2.0更改为1.6看看GWT 1.6 / 1.7文档)。注意这样做,你不必自己创建对话框。

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?

Daniel

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?");
      }
    });

(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.