Window.onbeforeunload does not start in Chrome, but works in Firefox

I have a main window and a popup. A popup window is created in the main window.

Like parent.php , the main window. On this page, I have a JavaScript function to reload the page, as shown below:

 function popUpClosed() { window.location.reload(); } 

We can open the popup from parent.php. Now I want to execute the popUpClosed() function on parent.php from the popup when we close / move the popup.

I tried the following methods to achieve the same.

Method 1

 window.onunload = window.onbeforeunload = function() { if(window.opener && !window.opener.closed) { window.opener.popUpClosed(); } }; 

Method 2

 window.onbeforeunload = Call; function Call() { if(window.opener && !window.opener.closed) { window.opener.popUpClosed(); } } 

Method 3

 window.onpagehide = function() { window.opener.popUpClosed(); } 

Everything works fine in every browser except Google Chrome. Chrome does not run any feature.

However, this has occurred in the last 2-3 days. Everything used to work well in Chrome before. (This may be due to the latest Chrome updates)

Any suggestion would be appreciated.

+5
source share
2 answers

You can try the following code:

I created a generic function to open a POP window:

 function PopupCenter(url, title, w, h) { var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left; var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top; width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width; height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height; var left = ((width / 2) - (w / 2)) + dualScreenLeft; var top = ((height / 2) - (h / 2)) + dualScreenTop; child = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left); if (child.focus) { child.focus(); } } 

To check the closed child window, I use the setInterval function and check the interval, as shown below:

 function checkChild() { if (child != undefined && child.closed) { //alert("Child window closed"); clearInterval(timer); location.reload(); } } 

Demo

-3
source

There is a problem with window.opener in the latest version of Google Chrome. The navigation of window.opener inside the beforeunload/unload handler beforeunload/unload ignored in the latest version. However, there is a workaround for this. Try entering the code:

 var btn = document.getElementById("btn"); function detectClick(event) { var wind = window.open('urlhere', 'name', 'width=' + screen.width/2 + ', height=' + screen.height/2); wind.onbeforeunload = function(){ popupClosed(wind); } } btn.addEventListener("click", detectClick, false); function popupClosed(wind) { setTimeout(function() { wind.opener.location.href = window.location.href; }, 0); } 
 <input id= "btn" type="button" value="open popup" /> 

THIS CODE DOES NOT EXECUTE HERE ON THE OVERFILL STACK, BUT WILL WORK AND BOTH: Chrome And Mozilla Firefox WHEN jsfiddle THROUGH jsfiddle OR SERVER.

Here is the working code: jsfiddle

here is a bug report link that confirms the beforeunload/unload handler problem.

+1
source

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


All Articles