gpt4 book ai didi

javascript - 关闭浏览器时打开自定义弹出窗口

转载 作者:行者123 更新时间:2023-11-28 07:08:43 25 4
gpt4 key购买 nike

当我尝试关闭页面或选项卡时,我想打开一个没有关闭选项的弹出窗口。

我正在使用这个脚本,但它不起作用。

<script language="JavaScript">
window.onbeforeunload = closepopup;
function closepopup(){
var id = '#dialog';

//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();

//Set heigth and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});

//transition effect
$('#mask').fadeIn(500);
$('#mask').fadeTo("slow",0.9);

//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();

//Set the popup window to center
$(id).css('top', winH/6-$(id).height()/6);
$(id).css('left', winW/2-$(id).width()/2);

//transition effect
$(id).fadeIn(2000);

}
</script>

最佳答案

不可能再阻止某人关闭浏览器(谢天谢地),但请看这里:how to block users from closing a window in javascript?

您需要从您的函数中返回 false,然后会弹出浏览器询问用户是否打算离开。您无法自定义此消息,但可以在其后面显示自定义弹出窗口。例如,以下代码在显示浏览器的标准“你想离开”提示之前将背景变为红色。

window.onbeforeunload = closepopup;
function closepopup(e){
document.bgColor="red";
return false;
}

关于javascript - 关闭浏览器时打开自定义弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32798935/

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