JavaScript event before loading does not work

I need to open a popup, then after closing the popup (refresh the parent page)

The jquery 'beforeunload' event does not work 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); } }); 

The 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 close the window:

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

Close window event does not work. Only in FireFox, Chrome, Opera.

+4
source share
3 answers

Try the following:

  /* * reload page */ windowParentRefresh: function(object) { setTimeout(function() { setTimeout(function() { $(object).bind('beforeunload', function() { object.opener.location.reload(); }); }, 1000); },1); } 
+2
source

I know this question is older than five years, but I recently had to solve the same problem. I used this code very well in Chrome, Firefox, Safari, Opera using jQuery 3.1.1:

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

However, this did not work in IE 11. I believe the reason is that event binding is not possible until the child window finishes loading. I discovered this when I found that it worked after I put a breakpoint on the line $().on . Violation there gave the child window the time needed to load.

Here's how I solved it: In the child window of the 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); 

That way, I wait until the child window is ready, and I know this because my user variable has been defined.

+1
source

The jQuery API specifically says that it is not bound to beforeunload, but instead is bound directly to the .onbeforeunload window.

 <script type="text/javascript"> window.onbeforeunload = askUser ; function askUser(){ return "Do you wanna quit?"; } </script> 
-1
source

Source: https://habr.com/ru/post/1381486/


All Articles