简体   繁体   中英

How can I provide a confirmation for closing an Internet Explorer window?

My company uses a web-based application that has long forms that the user fills out. Some of these forms save as you go (so that you can close the window and come back to it), but others can only be saved when they're completely filled out. We receive daily calls from users who "accidentally" clicked the close button on their IE window. Don't ask me how this happens, but the calls are constant since the system was implemented. I've been tasked with finding a solution.

I was initially asked if there was a way to remove the X entirely, but I don't think there is. So my idea is to somehow provide a prompt for users when they click the X, reminding them that their work may not be saved, or at the very least confirming that they want to close the window. Since I can't modify the application website in any way, I'd embed it in an HTML file that included code for detecting when the window was about to close. My questions are:

  1. Is this possible?
  2. How would I do this?
  3. Is there a better way?

By the way, I'm an application developer so I understand programming concepts, but I've never, ever done any web development.

Thanks in advance!

You can prompt the user by returning a string from your onBeforeUnload event handler. The browser will add its own message before and after the string you return as well as place the "OK" and "Cancel" buttons.

window.addEventListener('beforeunload', 
                        function() {
                          return 'Are you sure you want to close this window with unsaved changes?'; 
                        }, 
                        false);

You won't have any way of knowing if the user cancelled but this will prompt them to confirm that they want to close the window.

I think you're looking for this: http://msdn.microsoft.com/en-us/library/ms536907%28VS.85%29.aspx

function closeIt()
{
  return "Any string value here forces a dialog box to \n" + 
         "appear before closing the window.";
}
window.onbeforeunload = closeIt;

See also: https://developer.mozilla.org/en/DOM/window.onbeforeunload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM