Home > DeveloperSection > Forums > Jquery Show Popup on window closing
Pooja Malohtra
Pooja Malohtra

Total Post:47

Posted on    October-31-2014 12:09 AM

 JQuery JavaScript 

 1 Reply(s)
 756  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;




Alex Leblois
Alex Leblois

Total Post:67

Posted on    October-31-2014 12:12 AM

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;


Don't want to miss updates? Please click the below button!

Follow MindStick