繁体   English   中英

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

[英]Open custom pop when closing a browser

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

我正在使用此脚本,但无法正常工作。

<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> 

无法阻止他人再次(感谢)关闭浏览器,但请看这里: 如何阻止用户关闭JavaScript中的窗口?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM