Best way to detect browser closing/navigation to other page and do logout

前端 未结 4 925
夕颜
夕颜 2021-01-02 13:52

I am writing an application in GWT and I need to detect when a user navigates away from my application or when he closes the browser window (onUnload event) and do a logout

4条回答
  •  -上瘾入骨i
    2021-01-02 14:21

    This is how the closing event works:

    Window.addWindowClosingHandler(new Window.ClosingHandler()
    {
     @Override
     public void onWindowClosing(ClosingEvent event)
     {
      event.setMessage("Are you sure?");
     }
    });
    

    Then GWT gives the user a chance to say yes or no. Of course you can also add a test in there to see if they've got any unsaved data or whatever you want. Not setting the message or setting it to null doesn't do anything.

提交回复
热议问题