Need confirmation when closing the browser tab

Hi everyone,
Currently i am uploading large files to s3 cloud,This upload process will be done by a background thread.So I want to show a message to the user and ask a confirmation about the background threads when he or she is trying to close browser window.

Thanks in advance

Include the following javascript in your page

[code]
window.onbeforeunload = function (e) {
if (window.isUploadInProgress){
var message = “An upload is currently in progress. If you exit the page, the upload will stop. Are you sure you want to exit the page?”,
e = e || window.event;
// For IE and Firefox
if (e) {
e.returnValue = message;
}

    // For Safari
    return message;
}

};
[/code]Inside your code you can call

JavaScript.getCurrent().execute("window.isUploadInProgress = true;"); When the upload starts and the user needs the warning message

JavaScript.getCurrent().execute("window.isUploadInProgress = false;"); when the upload is no more in progress and the user can close the window

There could have been much more elegant solutions I think, but this is the quickest i found.

Regards
Guglielmo Moretti

Hi Guglielmo Moretti,
Thank you for your reply.
yes it will work but i am unable to keep the javascript code in my app because I am using vaadin 6.8 as portlet developement in liferay.
We can execute javascript code by executeJavascript method of window class but how to keep window.onbeforeunload = function (e) {…} in my portlet.
Please suggest this.
Thank you.

I think you’ll only need to call

getMainWindow().executeJavascript("<ALL_THE_JAVASCRIPT_FUNCTION_AS_STRING>"); and then the same code for setting the
isUploadInProgress
variable

getMainWindow().executeJavascript("window.isUploadInProgress = true;")

getMainWindow().executeJavascript("window.isUploadInProgress = false;")

Regards

Thank you alot,
It is working fine.

Regards
Srikanth