刷新前执行function
在HTML
页面上,当用户单击/按F5
按钮页面刷新但在刷新之前我想执行一个function或一个简单的警报。
用户可以单击刷新按钮,按F5
或Ctrl + R
使用核心JavaScript
, jQuery
或YUI
。
window.onbeforeunload = function(event) { return confirm("Confirm refresh"); };
$(window).bind('beforeunload', function(){ return '>>>>>Before You Go<<<<<<<< \n Your custom message go here'; });
资料来源: http : //www.mkyong.com/jquery/how-to-stop-a-page-from-exit-or-unload-with-jquery/
演示: http : //www.mkyong.com/wp-content/uploads/jQuery/jQuery-stop-page-from-exit.html
$(window).bind("beforeunload", function(){ return confirm("Do you really want to refresh?"); });
jQuery相关事件是:
$( window ).on('unload', function( event ) { console.log('Handler for `unload` called.'); });
对我很有用。