我试图在页面重新加载之前显示模式(带有消息和确认按钮)。
我必须检查一个数组:如果它是空的,我会刷新,如果不是,我想显示模式并询问用户是否要重新加载。
我尝试了这段代码:
$(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/