Javascript:在重新加载页面之前显示 Twitter-Bootstrap 模式

标签 javascript twitter-bootstrap reload onbeforeunload bootstrap-modal

我试图在页面重新加载之前显示模式(带有消息和确认按钮)。

我必须检查一个数组:如果它是空的,我会刷新,如果不是,我想显示模式并询问用户是否要重新加载。

我尝试了这段代码:

$(window).bind('beforeunload', function(){
            if(_pendent_annotations.length > 0){
                $('#change_document').modal('show');
                $("#change_doc_button").click(function(){
                                location.reload();
                });
                return false;       
            }

        });

问题是重新加载时会显示一个警报,其中包含以下消息:

"Are you sure you want to navigate away from this page?

false"

然后我可以选择离开或留下,如果我选择离开,它会重新加载,如果我选择留下,它会显示我的模式

如何避免警报并仅显示我的模式以供用户选择?

谢谢

最佳答案

试试这个: 您需要阻止事件返回任何内容。

window.addEventListener('beforeunload', function(event){
    if(_pendent_annotations.length > 0){
        $('#change_document').modal('show');
        $("#change_doc_button").click(function(){
              location.reload();
        });      
    }
});

https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload

关于Javascript:在重新加载页面之前显示 Twitter-Bootstrap 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28540175/

相关文章:

javascript - 是否可以在 KonvaJS 的形状/组上捕获右键单击事件?

javascript - 如何设置最小和最大剩余边距?

javascript - Bootstrap 3 下拉动画/缓动

css - Bootstrap : how to extend input to occupy available space?

iframe - 防止在回发时重新加载 iframe

javascript - 无法第二次读取json文件: "Unexpected end of input error"

javascript - 在 promise 中打开窗口

css - 右拉后底部对齐

python - 处理对象而不是模块重新加载的最佳方法

combobox - ExtJS:重新加载存储后的组合框未设置值