简体   繁体   English

window.onbeforeunload不在Chrome中触发,但在Firefox中工作

[英]window.onbeforeunload not firing in Chrome but working in Firefox

I have a main window and a pop up window. 我有一个主窗口和一个弹出窗口。 Popup window is created on the main window. 弹出窗口在主窗口中创建。

Like parent.php is the main window. parent.php一样是主窗口。 On this page, I have a JavaScript function to reload the page as below: 在这个页面上,我有一个JavaScript函数来重新加载页面,如下所示:

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

We can open popup window from parent.php. 我们可以从parent.php打开弹出窗口。 Now I want to execute popUpClosed() function on parent.php from popup window when we close / navigate the popup window. 现在我想在关闭/导航弹出窗口时从弹出窗口执行parent.php上的popUpClosed()函数。

I have tried following methods to achieve the same. 我尝试了以下方法来实现相同的目标。

Method 1 方法1

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

Method 2 方法2

window.onbeforeunload = Call;

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

Method 3 方法3

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

Everything is working is fine in every browser except Google Chrome. 除谷歌浏览器外,所有浏览器都能正常运行。 Chrome is not firing any of the function. Chrome没有触发任何功能。

However, this is happening from last 2-3 days. 但是,这发生在过去2-3天。 Earlier all the things was working well in Chrome. 之前所有的事情都在Chrome中运行良好。 (Might has been caused due to the latest Chrome updates) (可能是由于最新的Chrome更新引起的)

Any suggest will be appreciated. 任何建议将不胜感激。

Latest google Chrome has a problem with window.opener . 最新的谷歌浏览器有window.opener的问题。 window.opener navigation inside a beforeunload/unload handler is ignored in the latest version. 最新版本beforeunload/unload忽略beforeunload/unload处理程序中的window.opener导航。 However there is a workaround for this. 但是有一个解决方法。 Try below 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 WON'T EXECUTE HERE ON STACK OVERFLOW BUT WILL WORK IN BOTH: Chrome AND Mozilla Firefox WHEN EXECUTED THROUGH jsfiddle OR SERVER. 这个代码不会在这里执行堆栈溢出但是两种方式都可以工作: ChromeMozilla Firefox通过jsfiddle或SERVER执行时。

Here is a working code: jsfiddle 这是一个有效的代码: jsfiddle

here is the link of bug report that confirms beforeunload/unload handler issue. 是错误报告的链接,它确认了beforeunload/unload handler问题。

You can try the below code: 您可以尝试以下代码:

I have created the common function for open the POP up window: 我已经创建了打开POP up窗口的常用功能:

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();
        }
    }

For checking the child window closed or not I am using the setInterval function and check the interval like below: 为了检查子窗口是否关闭,我使用setInterval函数并检查如下的间隔:

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

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM