gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:36:22 27 4
gpt4 key购买 nike

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

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

我尝试了这段代码:

$(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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com