jQueryを使ってページを終了またはアンロードする方法
ページが終了またはアンロードされると、 ‘
unload
‘イベントがアクティブになりますが、このイベントはページの終了を停止できません。
$(window).bind('unload', function(){});
jQuery 1.4以降、 ‘
beforeunload
‘イベントを$(windows)オブジェクトにバインドして、ページの終了、アンロード、またはナビゲートを停止できます。
$(window).bind('beforeunload', function(){ return ''; });
‘
beforeunload
‘イベントが添付されている場合、ページを閉じる、戻るボタンを押す、またはページをリロードすると、本当に行きたいかどうかを尋ねる確認ボックスが表示され、OKをクリックしてページを終了します。ページを終了して同じページに留まります。
さらに、あなたは自分のメッセージを確認ボックスの中に追加することができます:
$(window).bind('beforeunload', function(){ return '>>>>>Before You Go<<<<<<<< \n Your custom message go here'; });
P.S ‘beforeunload’イベントは、jQuery 1.4
自分で試してみてください
<html> <head> <title>Refresh a page in jQuery</title> <script type="text/javascript" src="jquery-1.4.2.min.js"></script> </head> <body> <h1>Stop a page from exit with jQuery</h1> <button id="reload">Refresh a Page in jQuery</button> <script type="text/javascript"> $('#reload').click(function() { location.reload(); }); $(window).bind('beforeunload', function(){ return '>>>>>Before You Go<<<<<<<< \n Your custom message go here'; }); </script> </body> </html>
リンク://wp-content/uploads/jQuery/jQuery-stop-page-from-exit.html[デモを試してください]