How to catch user leaving a page and cancelling it

后端 未结 3 1277
萌比男神i
萌比男神i 2020-12-31 23:19

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\",

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 23:59

    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.

提交回复
热议问题