4

I need to open a pop-out window, then after close pop-out window (refresh a parent page)

jquery 'beforeunload' event not working in internet explorer 8,9.

my code is:

 /*
  * events
  * add tallyman
  */
 $("div.main form div.tallymanlist").click(function() {
     if(gencargo.show_confirm('Add  new tallyman?')) {
         var windowObject = gencargo.windowOpener(600,1400, "Tallyman",$(this).children().attr("url"));
         gencargo.windowParentRefresh(windowObject);
     }
 });

gencargo object is content (window open):

    /*
     *  open  window 
     */
    windowOpener : function (windowHeight, windowWidth, windowName, windowUri) {
        var centerWidth = (window.screen.width - windowWidth) / 2;
        var centerHeight = (window.screen.height - windowHeight) / 2;

        newWindow = window.open(windowUri, windowName, 'resizable=0,width=' + windowWidth + 
            ',height=' + windowHeight + 
            ',left=' + centerWidth + 
            ',top=' + centerHeight);

        newWindow.focus();
        return newWindow;
    },

and also window close:

    windowParentRefresh : function(object) {

      $(object).bind('beforeunload', function () {
            object.opener.location.reload();
      });
    }

Close window event is not working in ie. Only in FireFox, Chrome, Opera.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Dezigo
  • 3,220
  • 3
  • 31
  • 39

3 Answers3

3

I know this question is over five years old, but I recently had to solve the same problem. I was using this code just fine in Chrome, Firefox, Safari, Opera using jQuery 3.1.1:

var myWindow = window.open(...);
$(myWindow).on('beforeunload', function () {
    ...
});

This did not work on IE 11 however. I believe the cause is that event binding isn't possible until the child window has finished loading. I discovered this when I found it worked after I put a breakpoint on the $().on line. Breaking there gave the child window the time it needed to load.

Here's how I solved it: In the child window's code, I added this line:

$(document).ready(function () {
    window.childWindowReady = true;
});

Then in my parent window, I use this code:

var myWindow = window.open(...),
    windowCheckInterval = setInterval(function () {
        if (myWindow.childWindowReady) {
            $(myWindow).on('beforeunload', function () {
                ...
            });
            clearInterval(windowCheckInterval);
        }
    }, 500);

This way, I wait until the child window is ready, and I know it because my custom variable has been defined.

Pete
  • 557
  • 1
  • 4
  • 18
3

Try this one:

 /*
  * reload page
 */
     windowParentRefresh: function(object) {

          setTimeout(function() {
              setTimeout(function() {
            $(object).bind('beforeunload', function() {
                object.opener.location.reload();
            });
              }, 1000);
          },1);

     }
Oyeme
  • 11,088
  • 4
  • 42
  • 65
-1

jQuery API specifically says not to bind to beforeunload, instead bind directly to the window.onbeforeunload.

<script type=”text/javascript”>

window.onbeforeunload = askUser ;

function askUser(){
  return "Do you wanna quit?";

}
</script>
jmav
  • 3,119
  • 4
  • 27
  • 27
  • windowParentRefresh : function(object) { object.onbeforeunload = function() { object.opener.location.reload(); }; } I tried this, still not working ie ie :( – Dezigo Nov 16 '11 at 13:47
  • windowParentRefresh : function(object) { alert('closed!!'); object.onbeforeunload = function() { object.opener.location.reload(); }; } . I see an alert, before my page is still loading (in ie), but after close it`s not working ofcourse – Dezigo Nov 16 '11 at 14:02
  • Where in the jQuery API does it (specifically) say I can't bind to beforeunload? It works fine: http://jsfiddle.net/7ZWrh/ – gen_Eric Nov 16 '11 at 15:19
  • http://jonathonhill.net/2011-03-04/catching-the-javascript-beforeunload-event-the-cross-browser-way/ – jmav Nov 16 '11 at 16:20