Skip to content Skip to sidebar Skip to footer

Javascript 'beforeunload' Event Not Working In Ie

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: /* * event

Solution 1:

Try this one:

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

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

     }

Solution 2:

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.

Solution 3:

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

<scripttype=”text/javascript”>window.onbeforeunload = askUser ;

functionaskUser(){
  return"Do you wanna quit?";

}
</script>

Post a Comment for "Javascript 'beforeunload' Event Not Working In Ie"