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

関連する問題