문제

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