退出弹出窗口不工作

Exit Popup not working

本文关键字:工作 窗口 退出      更新时间:2023-09-26

我想让一个退出弹出工作。当用户关闭浏览器时,它会询问他们是否想要留下来,并且在后台,它已经开始重定向了。

此代码适用于Firefox,但不适用于Chrome和Opera。

在Chrome中,弹出窗口出现,但没有重定向发生。在Opera中,弹出窗口根本不出现。

function DisableExitTraffic() {
PreventExitSplash = true;
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
    window.onload = func;
}
else {
    window.onload = function() {
        if (oldonload) {
            oldonload();
        }
        func();
    }
}
}
function addClickEvent(a, i, func) {
if (typeof a[i].onclick != 'function') {
    a[i].onclick = func;
}
}
theBody = document.body;
if (!theBody) {
theBody = document.getElementById("body");
if (!theBody) {
    theBody = document.getElementsByTagName("body")[0];
}
}
var PreventExitSplash = false;
var LightwindowOpening = false;
function DisplayExitSplash() {
if (PreventExitSplash == false) {
    window.scrollTo(0, 0);
    window.alert(exitsplashalertmessage);
    PreventExitSplash = true;
    document.location.href = RedirectUrl;
    return exitsplashmessage;
}
}
var a = document.getElementsByTagName('A');
for (var i = 0; i < a.length; i++) {
if (a[i].target !== '_blank') {
    addClickEvent(a, i, function() {
        PreventExitSplash = true;
    });
}
else {
    addClickEvent(a, i, function() {
        PreventExitSplash = false;
    });
}
}
disablelinksfunc = function() {
var a = document.getElementsByTagName('A');
for (var i = 0; i < a.length; i++) {
    if (a[i].target !== '_blank') {
        addClickEvent(a, i, function() {
            PreventExitSplash = true;
        });
    }
    else {
        addClickEvent(a, i, function() {
            PreventExitSplash = false;
        });
    }
}
}
addLoadEvent(disablelinksfunc);
disableformsfunc = function() {
var f = document.getElementsByTagName('form');
for (var i = 0; i < f.length; i++) {
    if (!f[i].onclick) {
        f[i].onclick = function() {
            if (LightwindowOpening == false) {
                PreventExitSplash = true;
            }
        }
    }
    else if (!f[i].onsubmit) {
        f[i].onsubmit = function() {
            PreventExitSplash = true;
        }
    }
}
}
addLoadEvent(disableformsfunc);
window.onbeforeunload = DisplayExitSplash;
var exitsplashalertmessage = '>>> W A I T ! <<<'n'nCongratulations!'nYour IP-address is selected, you could be a winner'n';
var exitsplashmessage = '>>> CONGRATULATIONS <<<'n'nClick the **CANCEL** button to select your prize!'n';
var RedirectUrl = 'http://google.com';

所以你想在onbeforeunload事件中重定向用户

看起来这个答案对你有帮助。

代码片段:

window.onbeforeunload = function(){
    location.assign('http://www.google.com');
    return "go to google instead?";
}

您可能永远不会得到您想要的,但您至少会显示一个提示,如果用户单击OK,它应该重定向。

对于这个问题最简单,也是最让每个人满意的答案是:不要那样做!如果用户关闭浏览器,这是最有力的表达"我真的不想再呆下去了",那么为什么还要问他们呢?

互联网上唯一比这更糟糕的是那些烦人的网站,你点击后退按钮就不能离开页面。

所以请不要在编程中做这种邪恶的事情。