0

I have a content management page on my CMS on which I have a popup that warns the user on page close. It is a very simple warning implemented like so:

window.onbeforeunload = function (e) {
     return 'Are you sure you want to close?';
} 

It works, but the problem is that the popup is shown even when the user clicks the same button. I am very new to document events so I have no idea how to do this, but I want the popup to only show when the page is being closed and not when it is being posted back.

Also I have not used JQuery in my website so please don't suggest it; I don't want to use a whole framework just for a popup. . Thanks.

TheGateKeeper
  • 4,420
  • 19
  • 66
  • 101
  • 1
    Take a look to this to detect a postback: http://stackoverflow.com/questions/1857606/how-to-detect-track-postback-in-javascript – Adriano Repetti Nov 27 '12 at 13:27
  • That questions only shows you how to detect if a page has been posted back, it cannot be used to detect if a user clicked a button that causes a postback. See my answer below. – TheGateKeeper Nov 27 '12 at 14:22

1 Answers1

0

I was able to get it to work by using a global variable, like so:

var check = true;

Which is checked for when showing the popup:

    window.onbeforeunload = function (e) {
        if (check) {
            return 'Are you sure you want to close?';
        }
    }

Then you simply set check to false when you click a button that does a postback. Hope this helps someone.

TheGateKeeper
  • 4,420
  • 19
  • 66
  • 101