Preventing onbeforeunload dialogs in IE9

北城以北 提交于 2019-12-02 11:29:16

问题


I'm having an issue in IE9 with onbeforeunload -- when the code below is run it repeatedly brings up a dialog asking if you want to stay or leave the page.

I modified my code to prevent default functioning based on this thread to no avail.

Any help on this would be much appreciated.

Code Sample:

window.onbeforeunload = function(e) {
    e.preventDefault();
    e.returnValue = false;
    saveFormData();
    return null;
}

function saveFormData() {
    $.post("<?php echo site_url('resume/cleanup'); ?>", { resume_id: "<?php echo $this->session->userdata('resume_id'); ?>" } );
}

回答1:


You cannot send an AJAX request while the page is unloading as most browsers block it. You should ask the user to stay on the page if there is dirty data. That's all you should do from your onbeforeunload handler

Try your code without calling $.post and it should behave as expected




回答2:


The only thing you are allowed to do in an onbeforeunload handler is display a dialog. You specifically don't get any extra time to perform tasks which may take time to complete, like firing off HTTP requests — the user has asked to leave your site; aside from asking them politely whether they want to stay, you aren't allowed to make them stay (even while you clean up).

https://developer.mozilla.org/en/DOM/window.onbeforeunload



来源:https://stackoverflow.com/questions/9985712/preventing-onbeforeunload-dialogs-in-ie9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!