You are looking for the onbeforeunload event.
as
$(window).bind('beforeunload', function(){ return "Are you really sure?"; });
Native
window.onbeforeunload = function(){ return "Are you really sure?"; });
This, of course, is simply a "prevention method." You still need logic to know if there have been changes to your site. This is easy to do, for example, using boolean . In addition, you should do a quick detection, for example
if('onbeforeunload' in window){}
I think that all major browsers support this event these days, but there is still a browser that does not know this event. Therefore, if the above condition fails, you can still retreat to another path.
jAndy
source share