2016-08-05 13 views
0

ユーザーがページを離れるときを検出しようとしています。 .on( "unload"、fn)メソッドはそれを行うべきだと思った:jQuery .on( "unload"、fn)が私のために働いていない

$(document).ready(function(){ 
    $(window).on("unload", function(){ 
    alert("Goodbye!"); 
    }); 
}); 

しかし、それは動作していないようです。ページから離れて移動すると何も起こりません。ただし、「アンロード」を「ロード」に変更すると、ページが読み込まれたときに警告ボックスが適切に表示されます。

これは正しい方法ですか?たぶん

答えて

1

http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm

から撮影

<script language="JavaScript"> 
    window.onbeforeunload = confirmExit; 
    function confirmExit() 
    { 
    return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; 
    } 
</script> 

は廃止行くようだ "アンロード" 1、よりイベントより信頼性が高いだろう "beforeunload"。ライブデモ作品

$(document).ready(function(){ 
     window.onbeforeunload = confirmExit; 
     function confirmExit() 
     { 
     return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; 
     } 
}); 
+0

が、jQueryのは、「onbeforeunload」について知っていないようだ。私はまた、あなたのケースでそうjQueryの

からよりも、JavaScriptから呼び出されたときに「beforeunload」イベントは、より信頼性があったことを読んで方法。 : –

+0

JavaScript関数でjQueryコードを記述することができます(注意してください)。別の言い方をすると、javascriptでこのイベントをアタッチして、通常のjQueryの処理を行います。 – technico

+0

あなたのケースに合わせてコードが追加されます – technico

関連する問題