When the user closes the tab or refreshes the page, the site must display a popup to confirm this. I tried this code:
window.onbeforeunload = function (e) {
return confirm("Are you sure you want to leave this page?");
};
This didn't work in either firefox or chrome. In firefox no popup came up. And in chrome the default one didn't get overridden either. I even tried using the following code but to no avail:
window.onbeforeunload = function (e) {
var dialogText = 'Are you sure about this?';
e.returnValue = dialogText;
return dialogText;
};
How do I solve this issue? Any code snippets would be helpful. Thank you.
I found this snippet on the internet:
window.onbeforeunload = function (e) {
return "Please click 'Stay on this Page' if you did this unintentionally";
};
This is working perfectly as required. I found out that you actually don't need to add any confirm call. If you just return a string, this will prompt the browser to confirm leaving the page. Most commercial browsers have this functionality supported by default.