Reputation: 19559
when user click a another link or refresh the page, the window will unload, how to prevent/confirm it?
Upvotes: 4
Views: 6654
Reputation: 190907
From here http://bytes.com/topic/javascript/insights/825556-using-onbeforeunload-javascript-event
<script type="text/javascript">
window.onbeforeunload = function(){ return 'Your Returned String Goes Here';}
</script>
Upvotes: 7
Reputation: 20712
I think the onbeforeunload event handler would be what you're looking for.
Try the following:
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
Upvotes: 3