退出弹出窗口无法正常工作

时间:2011-07-25 04:56:46

标签: javascript

我正试图让退出弹出窗口正常工作。当用户关闭浏览器时,它会询问他们是否要留下,并且在后台,它开始重定向。

此代码适用于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';

2 个答案:

答案 0 :(得分:1)

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

看起来this answer可以帮到你。

代码段:

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

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

答案 1 :(得分:0)

最简单的,对每个人来说最满意的答案都是:不要那样做!如果用户关闭的浏览器是“我真的不想再留下”的最强大的表达方式,那么为什么要再次询问呢?

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

所以请不要用编程来做这些邪恶的事情。