刷新前执行功能
问题描述:
在HTML
页面,当用户点击/按F5
按钮页面刷新但在刷新之前我想执行一个功能或一个简单的警报。刷新前执行功能
用户可以点击刷新按钮,按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
+3
在Firefox上不起作用。 – vsync 2014-10-06 16:38:16
答
了jQuery相关的事件是:
$(window).on('unload', function(event) {
console.log('Handler for `unload` called.');
});
篦作品对我来说。
[Javascript function,on web page close]可能的重复(http://*.com/questions/2858057/javascript-function-on-web-page-close) – Shoban 2012-02-16 09:13:04