I want to pop up a message asking for confirmation if the user wants to leave the current page, exactly like stackoverflow does it when you are typing a question.
Could someone help me on this, by giving me a script or redirecting me to an answer (on this site or elsewhere) ?
Thanks
You could use the onbeforeunload
event.
window.onbeforeunload = function() {
return 'Are you sure that you want to leave this page?';
};
You need to handle the window's onbeforeunload
event and return a confirmation message as a string.
onunload
will not work here. Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With