欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JS捕获窗口关闭事件

程序员文章站 2022-06-04 07:57:48
...
<script type="text/javascript">  

    window.onbeforeunload = onbeforeunload_handler;  
    window.onunload = onunload_handler;  

    function onbeforeunload_handler(){  

        var warning="确认退出?";          

        return warning;  

    }  
      
    function onunload_handler(){  

        var warning="谢谢光临";  

        alert(warning);  

    }  
</script>

window.onbeforeunload = onbeforeunload_handler;  
    window.onunload = onunload_handler;  

这两种都可以捕获窗口关闭事件,window.onbeforeunload 在window.onunload之前执行