2011-07-25 136 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