How to control browser confirmation dialog on leaving page?

Here's what I've done, modify to fit your needs:

// This default onbeforeunload event
window.onbeforeunload = function(){
    return "Do you want to leave?"
}

// A jQuery event (I think), which is triggered after "onbeforeunload"
$(window).unload(function(){
    //I will call my method
});

Note: it's tested to work in Google Chrome, IE8 and IE10.


This is simple. Just use

window.onbeforeunload = function(){
  return '';
};

to prompt when the user reloads, and

window.close = function(){
 return '';
};

to prompt when the user closes the page. But the user have to click on the page once, or do anything on the page for the code to detect. You don't have to put anything the the return'';, because JavaScript interpreter would just ignore it.