开发者

Exit Popup not working

I'm trying to get an exit popup to work. When user closes browser, it asks them if they want to stay or not, and in the background, it starts to redirect already.

This code works in Firefox, but n开发者_开发技巧ot in Chrome and Opera.

In Chrome, the popup appears but no redirect happens. In Opera, the popup doesn't appear at all.

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';


So you want to redirect the user inside an onbeforeunload event.

It looks like this answer could help you.

Code snippet:

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

You probably won't ever get exactly what you want, but you'll at least display a prompt, and if the user clicks on OK, it should redirect.


The easiest, and for everyone most satisfying answer to this question is: don't do that! If a user closes a browser that is the most powerful expression of "i really don't want to stay anymore" so why ask them again?

The only thing worse on the internet are those annoying sites where you click the back button and can't leave the page.

So please don't do such evil evil things with programming.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜