简体   繁体   English

仅在浏览器关闭时触发功能

[英]Fire a function only on browser close

The idea is to show a customized popup / div when user closes the browser. 想法是当用户关闭浏览器时显示自定义的弹出窗口/ div。 Found this code online and it works for me partially. 在网上找到了此代码,部分对我有用。

JS: JS:

window.onbeforeunload = function (e) {            
            e = e || window.event;
            if (e) {
                e.returnValue = '';
                $("#popup").show();
            }            
        }

HTML: HTML:

<div id="popup" class="popup">
</div>

When user closes the browser, an alert pops up with a confirmation, to leave or stay. 当用户关闭浏览器时,会弹出一条警告,提示您离开或停留。 Is it possible to show the popup/div without showing the default JS alert. 是否可以在不显示默认JS警报的情况下显示弹出窗口/ div。

This piece of code also fires when user refreshes the page. 用户刷新页面时也会触发这段代码。 How to allow this only on close event. 如何仅在关闭事件上允许此操作。

Only works in the more recent browsers to ignore the alert with a return if you don't use jquery: 如果您不使用jquery,则仅在较新的浏览器中起作用,以忽略返回的警报:

window.onbeforeunload = function (e) {            
     e = e || window.event;
     if (e) {
         $("#popup").show();
     }

    return null;        

}

Events (e), actually never should be edited 事件(e),实际上永远不应该编辑

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

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