在我的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';
});
$(window).bind("beforeunload", function(){
return confirm("Do you really want to refresh?");
});
与jQuery相关的事件是:
$( window ).on('unload', function( event ) {
console.log('Handler for `unload` called.');
});
对我来说很有效。