Javascript 退出弹出窗口和内部链接

Javascript 退出弹出窗口和内部链接,javascript,Javascript,当用户退出页面时,我有一个退出弹出脚本 var triedToLeave = false; function onBeforeUnload(){ triedToLeave = true; return "you can put your own message here"; } setInterval( function (){ if( triedToLeave ){ window.removeEventListener( "beforeunload"

当用户退出页面时,我有一个退出弹出脚本

var triedToLeave = false;
function onBeforeUnload(){
    triedToLeave = true;
    return "you can put your own message here";
}
setInterval( function (){
     if( triedToLeave ){
         window.removeEventListener( "beforeunload", onBeforeUnload, false );
         document.location.href = "http://stackoverflow.com/";
     }
}, 2000 );
window.addEventListener( "beforeunload", onBeforeUnload, false);

但是,如果您单击站点内的链接,则会触发弹出窗口。有人能告诉我需要添加什么来防止内部链接触发退出弹出窗口吗?提前非常感谢。

在卸载前将单击处理程序添加到禁用
处理程序的所有链接

function removeBeforeUnload() {
    window.removeEventListener( "beforeunload", onBeforeUnload, false );
}

window.onload = function() {
    var allLinks = document.getElementsByTagName('a');
    for (var i = 0; i < allLinks.length; i++) {
        allLinks[i].addEventListener("click", removeBeforeUnload, false);
    }
};
函数removeBeforeUnload(){
removeEventListener(“beforeunload”,onBeforeUnload,false);
}
window.onload=函数(){
var allLinks=document.getElementsByTagName('a');
对于(var i=0;i
您需要在卸载前向所有删除
侦听器的链接添加一个单击处理程序。您能更具体一点吗?我是否将该代码添加到当前javascript代码的末尾?加载文档后,您可以将其放在任何位置。我似乎无法让它工作。我在弹出代码之后添加了代码,但它不起作用。对不起,我对javascript不是很在行。我已经将它移动到
窗口。onload
处理程序中。看看这是否有帮助。巴尔马你是个救生员!非常感谢你的帮助!Stack Overflow上的用户总是非常乐于助人,非常友好。谢谢