$('body').bind('beforeunload',function(){
//do something
});
But this wont save any info for later, unless you were planning on saving that in a cookie somewhere (or local storage) and the unload
event does not always fire in all browsers.
Example: http://jsfiddle.net/maniator/qpK7Y/
Code:
$(window).bind('beforeunload',function(){
//save info somewhere
return 'are you sure you want to leave?';
});