Confirm exit of page

A simpler solution would be to use window.onbeforeunload.

window.onbeforeunload = confirmExit;
function confirmExit()
{
if( <check for anything unsaved>)
{
return “This is your warning message.”;
}
}

It will show a warning message and will stay on your page if user clicks ‘cancel’

see for complete solution
how-do-we-do-it.blogspot