JQUERY SHOW POPUP ON WINDOW CLOSING

Pooja Malohtra

Total Post:47

Points:331
Posted by  Pooja Malohtra
 1203  View(s)
Ratings:
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>

<script>

    $(document).ready(function () {

        $(window).bind('beforeunload', function (e) {

            $("#lead-gen-modal").dialog("open");

            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;

        });

    });

</script>

  1. Alex Leblois

    Post:67

    Points:471
    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)

            $("#lead-gen-modal").dialog("open");

    });

    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;

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!