You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfirmBrowserTabClose.java 931B

12345678910111213141516171819202122232425
  1. package com.vaadin.tests.application;
  2. import com.vaadin.server.VaadinRequest;
  3. import com.vaadin.tests.components.AbstractTestUIWithLog;
  4. import com.vaadin.ui.Button;
  5. import com.vaadin.ui.JavaScript;
  6. public class ConfirmBrowserTabClose extends AbstractTestUIWithLog {
  7. @Override
  8. protected void setup(VaadinRequest request) {
  9. // To test the behavior, do
  10. // 1. Open the test in the browser
  11. // 2. Close the browser tab/window
  12. // 3. Choose to stay on the page after all
  13. // 4. Click the button
  14. // There should be no error
  15. Button b = new Button("Say hello", event -> log("Hello"));
  16. addComponent(b);
  17. JavaScript.eval("window.addEventListener('beforeunload', function (e) {"
  18. + "var confirmationMessage = 'Please stay!';"
  19. + "e.returnValue = confirmationMessage;"
  20. + "return confirmationMessage;" + "});");
  21. }
  22. }