0

I want to show an alert message when a user tries to close the window or tab. How to achieve this? I have tried the code below but I am unable to solve the problem

<!DOCTYPE html>
<html>
<body>

<p>This example uses the addEventListener() method to attach a "beforeunload" event to the window object.</p>

<p>Close this window, press F5 or click on the link below to invoke the beforeunload event.</p>


<script>
 var unloadEvent = function (e) {
        var confirmationMessage = "Warning: Leaving this page will result in any unsaved data being lost. Are you sure you wish to continue?";

        (e || window.event).returnValue = confirmationMessage; //Gecko + IE
        return confirmationMessage; //Webkit, Safari, Chrome etc.
    };
    window.addEventListener("beforeunload", unloadEvent);
</script>

</body>
</html>
TheEdge
  • 9,291
  • 15
  • 67
  • 135
Testing Anurag
  • 603
  • 4
  • 13
  • 26
  • 1
    Possible duplicate of [How can I override the OnBeforeUnload dialog and replace it with my own?](https://stackoverflow.com/questions/276660/how-can-i-override-the-onbeforeunload-dialog-and-replace-it-with-my-own) – Superdrac Jan 07 '19 at 10:42
  • Refer this, > https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing – Thanveer Shah Jan 07 '19 at 11:17
  • Get reference from this https://stackoverflow.com/questions/1119289/how-to-show-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-ch – nature vetri Jan 07 '19 at 11:17

0 Answers0