退出弹出重定向,但仅在退出时执行,而不是在单击页面上的任何其他 html 重定向按钮时执行


Exit Popup Redirect but only execute on exit not on clicking any other html redirect button on page

l 我正在使用这段代码

var exitPop = false;
var nonFire = false;
window.onbeforeunload = function () {
    if(!exitPop){
        exitPop=true;
        return 'Wait! YOU ARE TODAYS WINNER!';
    }
};
setInterval(function(){
    if(exitPop && !nonFire){
        nonFire = true;
        window.location.href = 'http://google.com';
    }
}, 200);

但它也会在单击页面上的任何 HTML 重定向按钮时执行.. 我希望它仅在有人关闭浏览器时才执行,并且它应该支持所有浏览器。

我只需要在我的网站中的一个链接中添加它,ID 该怎么办? 我的意思是我正在使用此代码进行重定向

<script type="text/javascript"> 
window.fbAsyncInit = function() {
    FB.Event.subscribe('comment.create',
    function (response) {
        window.location = "http://domain.com";
    });
    FB.Event.subscribe('comments.remove',
    function (response) {
        window.location = "http://domain.com";
    });   
};
(function() {
    var e = document.createElement('script');
    e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
}());
//]]>
</script>

所以我希望退出函数不为此执行.. 那么如何集成这个

我还没有测试过它,但您应该能够将"click"事件附加到页面上的每个链接,该事件应该设置一个全局变量,例如

linkClicked = true;

然后,您可以在卸载事件中检查该变量

if (!linkClicked) // variable is false so they must not have clicked on a link
{
    // Some annoying message here
}

免责声明:这几乎是伪代码,它不是复制+粘贴解决方案。

window.onbeforeunload

区分链接、后退/前进按钮、退出按钮或其他任何东西。 如果在您离开页面时触发,无论您如何离开页面。