My Code:

//hold window open on form change
window.onbeforeunload = function(e) {
    if(formChanges > 0) {
        if(formData != initFormData) {
            if(confirm('here')) {
                e.preventDefault();
            }
            else {
                e = null;
            }
        }
        else {
            e = null;
        }
    }
    else {
        e = null;
    }
};

The three vars (formChanges, formData, and initFormData) are all being filled correctly, and little tests have shown that they are being read correctly within the function. The problem is that the page unloads nomatter what, and no confirmation dialog ever appears.

The console log flashes for a moment before being unloaded (I can't seem to write it's contents to file) and I can see the message Blocked confirm 'here' during beforeunload, but it's gone before I can access it. Any help is appreciated!

有帮助吗?

解决方案

WHen using onbeforeunload you have to return a string, like so:

window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};

source: https://developer.mozilla.org/en-US/docs/Web/API/Window.onbeforeunload

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top