Jquery Show Popup on window closing

Total Post:47

 1613  View(s)
Rate this:

I am trying to display a confirmation message when the user closes the browser/tab and not when any of the links on page is clicked.

I have got the first part of displaying the message on window close working nicely but problem is that the message/alert is displayed also when user clicks on any link on the page.

I have got code from somewhere to prevent this behavior but still when ever any link is clicked the alert pops up.

Here is the code:

<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/backfix-min.js"></script>
    $(document).ready(function () {
        $(window).bind('beforeunload', function (e) {
            return "Wait\! Don\'t Go\! We have a special offer for you\. Stay on this page to receive big savings\!";
        $("a").click(function () {
            window.onbeforeunload = null;

  1. Post:67

    Re: Jquery Show Popup on window closing

    just use a global variable and set it to false when clicking a link.

    var key = true;
    $(window).bind('beforeunload', function (e) {
         if (key)

    update :

    $(document).on("click", "a", function () {
         key = false;

    or if you just want to prevent closing window you can do this :

    window.onbeforeunload= function(e) {
         if (key)
             return false;

      Modified On Apr-06-2018 02:01:08 AM