Execute function before refresh
问题 On my HTML page, when the user clicks / presses F5 button the page refreshes, but before it refreshes I want to execute a function or a simple alert. User can click on refresh button, press F5 or Ctrl + R . Using core JavaScript, jQuery or YUI. 回答1: window.onbeforeunload = function(event) { return confirm("Confirm refresh"); }; 回答2: $(window).bind('beforeunload', function(){ return '>>>>>Before You Go<<<<<<<< \n Your custom message go here'; }); Source: http://www.mkyong.com/jquery/how-to