最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - JavaScript 'beforeunload' event not working in ie - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Nov 16, 2011 at 15:17 gen_Eric 227k42 gold badges303 silver badges342 bronze badges asked Nov 16, 2011 at 13:23 DezigoDezigo 3,2563 gold badges33 silver badges39 bronze badges 2
  • yes. I have tried it. but it`s still not working in ie – Dezigo Commented Nov 16, 2011 at 13:43
  • It seems to work OK for me on IE 9, but not in IE 8 (IE 9 in IE 8 mode). jsfiddle/DQfMH – gen_Eric Commented Nov 16, 2011 at 15:40
Add a ment  | 

3 Answers 3

Reset to default 3

Try this one:

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

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

     }

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.

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>
发布评论

评论列表(0)

  1. 暂无评论