Harish
Harish

Reputation: 3483

Identifying X close event of a popup window using javascript

I need to differentiate between user driven close of a popup window using X close button and close through code.

var win= window.showModelessDialog("http://localhost/test/test.aspx",'google,....);
//Some manipulations            
//Manipulation ends
if(win!=null && win.open)
{
 win.close();
}

Now I have full access over test.aspx and test.aspx.cs.I have a onbeforeunload method defined in test.aspx page which will be called either way I close the window(X close or my code gets executed)I basically want to differentiate my X close and programmatic close so that I can do some backend manipulations

Upvotes: 0

Views: 2114

Answers (3)

Andreas
Andreas

Reputation: 21911

// parent
function closePopup(win) {
    win.close();
    // do the magic stuff...
}


// popup (test.aspx)
function closeMe() {
    self.opener.closePopup(window);
}

Update
As of your comment, just check the closed property of the popup. If it is false, the popup is still open, otherwise it has already been closed

if (win.closed === false) {
    win.close();
    // do magic stuff here
}

Upvotes: 1

xCander
xCander

Reputation: 1337

Something like this perhaps:

var MyPopup = {

  _win : null,

  _userClosingWindow : true,

  open : function() {
    var _this = this;
    this._win = window.open(...);
    this._win.onbeforeunload = function() {
      if( _this._userClosingWindow ) {
         // closed by user
      }
      else {
        // closed in code
      }
    };
  },

  close : function() {
    this._userClosingWindow = false;
    this._win.close();
  }

};

Then you can use MyPopup.open() and MyPopup.close() and still know when the close function is called or when the popup is closed by the user.

Upvotes: 1

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

Use Model Popup & include "OK" & "Cancel" Button.

Now you can handle both the "OK" & "Cancel" Button Events.

you can use:

AjaxControlToolkit - ModalPopup

jQuery UI - Dialog

Upvotes: 1

Related Questions