2011-07-25 140 views
-3

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

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

这是非常糟糕,凌乱和过时的代码 - 在这个阶段,您可能从学习像jQuery这样的东西中获益。 – thomasrutter

回答

1

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

它看起来像this answer可以帮助你。

代码片段:

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

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

0

最简单的,对于每个人来说,最令人满意的答案是:不要这样做!如果用户关闭最强大的“”的浏览器,我真的不想再呆“那么为什么再问一次呢?

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

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

+0

那又何必问他们呢?简而言之,增加销售额。 – JohnWolf