Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

TUTORIALVaadin lets you build secure, UX-first PWAs entirely in Java.
Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Calling javascript synchronously by Enver Haase, 1 month ago
Detect tab/window/broswer close event
Hello,
I want to detect the window/tab close event, to clear some server side parameters and initialize the connection, i found this code in the forum, but i have 2 problemes:
1. When i close the tab, ok, but i don't receive the event when i close the application firefox directly.
2. When i create a sub window to show the detail information or to download a file on the main page, the event is also sent, why?
Thank you.
MainUI:
this.addDetachListener(new DetachListener() {
public void detach(DetachEvent event) {
System.out.println("######### Detached ##########");
}
});
JavaScript.getCurrent().addFunction("aboutToClose", new JavaScriptFunction() {
@Override
public void call(org.json.JSONArray arguments)
throws org.json.JSONException {
System.out.println("Window/Tab is Closed.");
//TODO Call Method to Clean the Resource before window/Tab Close.
}
});
Page.getCurrent().getJavaScript().execute("window.onbeforeunload = function (e[code]) { var e = e || window.event; aboutToClose(); return; };");
[/code]
Last updated on
This work:
String js = "window.addEventListener(\"beforeunload\", function () {"+
"aboutToClose();"+
"});";
Page.getCurrent().getJavaScript().execute(js);
Last updated on
You cannot reply to this thread.