最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Troubleshoot of beforeunload confirm call - Stack Overflow

programmeradmin3浏览0评论

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!

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!

Share Improve this question asked Feb 12, 2014 at 11:12 EamonnEamonn 1,3202 gold badges23 silver badges54 bronze badges 2
  • possible duplicate of How to show a modal dialog before beforeunload shows its own? – Denys Séguret Commented Feb 12, 2014 at 11:13
  • This is old, but a tip for people debugging. In the console (at least in Google Chrome) under settings check Preserve log this will keep the "flashed" errors so you can see them. – Chad Commented Dec 14, 2017 at 19:58
Add a ment  | 

1 Answer 1

Reset to default 6

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

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

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

发布评论

评论列表(0)

  1. 暂无评论