So I'm trying to implement code to prompt the user before they navigate away from the page while an upload is in progress. I attempted to use onbeforeunload and the prompt worked but it appears to kill the upload (at least in FF3). Is there a way to make unbeforeunload work or an alternative that doesn't involve putting special code on every link I have on the page?
Here is my code, though I plan to make it more robust to actually detect what state the swfu object is in:
onbeforeunload_handler = (function() {
//Called whenever document.unload
//Also called by some AJAX framework functions
if (document.swfu != null)
return 'All uploading videos will be LOST. Are you sure you want to leave?';
});
window.onbeforeunload = onbeforeunload_handler;
swfu is a global instance declared in the main page as such:
document.swfu = new SWFUpload(settings);
June 11, 2009 - 11:05am
bump
June 16, 2009 - 4:29pm
has anyone tried to do this?
June 26, 2009 - 3:08pm
Can I at least get an official "you are doing it wrong" or a "sorry haven't tried that before"? I'm not feeling the swfUpload support that I would like here.
June 26, 2009 - 5:30pm
We haven't tried that before. We did some testing with onbeforeunload when the File Selection dialog is visible but couldn't prevent the crash caused by leaving the page (or unloading the Flash movie) with the dialog open.
December 10, 2009 - 3:44am
What's the solution for this?
April 24, 2010 - 11:01pm
Here's the code I tried to use; as described in the original post, displaying the confirmation dialog completely interrupts the upload, and cancelling the window close doesn't restore it.
// m_uploader = new SWFUpload({ ... });
var isUploadInProgress = function () {
if (!m_uploader) {
return false;
}
var stats = m_uploader.getStats();
return (stats && stats.files_queued > 0);
};
window.onbeforeunload = function () {
if (isUploadInProgress()) {
return "Navigating away from this page will stop uploads in progress. Are you sure?";
} else {
return null;
}
};
April 24, 2010 - 11:25pm
OK, I don't think I changed anything in the code, but now it seems to be working. So maybe this problem is intermittent?