2

このコードにより、Internet Explorer に「終了してもよろしいですか?」というメッセージが表示されます。dirtyMessage が null の場合でも、毎回メッセージを送信します。私は何を間違っていますか?

$(window).bind('beforeunload', function() {
  var dirty = didUserUpdateSomething();  

  if (dirty) {
    return "You will lose your changes.";
  }
  return null;
});
4

2 に答える 2

0

The beforeunload in IE is a bit quirky; Returning ANYTHING in your event handler (even null) will cause it to pop up a box asking the user if the user is quite sure they want to leave the page.

You can try this as a possible workaround:

var beforeUnloadFired = false;
$(window).bind('beforeunload', function (event) {
    if (!beforeUnloadFired) {
        beforeUnloadFired = true;
        event.returnValue = "You have attempted to leave this page.  Are you sure you want to navigate away from this page?";
    }
    window.setTimeout(function () {
        ResetUnloadFired();
    }, 10);
});

function ResetUnloadFired() {
    beforeUnloadFired = false;
}

Couple of references: https://developer.mozilla.org/en-US/docs/DOM/Mozilla_event_reference/beforeunload?redirectlocale=en-US&redirectslug=Mozilla_event_reference%2Fbeforeunload

and

http://msdn.microsoft.com/en-us/library/ie/ms536907(v=vs.85).aspx

NOTE: see the "Remarks" for pertinent information within that second reference.

于 2013-05-13T18:20:48.953 に答える