Question

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!

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top