18

我在堆栈溢出中搜索了许多问题,并且可能在此处 重复Detect Popup

但是not helped for meChrome中测试时(测试 v26.0.1410.64)
遵循方法Worked in IE and Firefox但是not in Chrome

var popup = window.open(winPath,winName,winFeature,true);
 if (!popup || popup.closed || typeof popup.closed=='undefined'){
       //Worked For IE and Firefox
        alert("Popup Blocker is enabled! Please add this site to your exception list.");
        window.location.href = 'warning.html';
 } else {
        //Popup Allowed
        window.open('','_self');
        window.close();
} 

还有更好的适用于 Chrome 的解决方案吗?

4

6 回答 6

21

最后,它通过结合来自 Stackoverflow 成员的不同答案而成功
此代码对我有用并经过测试IE, Chrome & Firefox

var popup = window.open(winPath,winName,winFeature,true);
 setTimeout( function() {
    if(!popup || popup.outerHeight === 0) {
        //First Checking Condition Works For IE & Firefox
        //Second Checking Condition Works For Chrome
        alert("Popup Blocker is enabled! Please add this site to your exception list.");
         window.location.href = 'warning.html';
    } else {
        //Popup Blocker Is Disabled
        window.open('','_self');
        window.close();
    } 
}, 25);
于 2013-05-14T11:04:40.913 回答
3

试试下面..!!

var pop = window.open("about:blank", "new_window_123", "height=150,width=150");

// Detect pop blocker
setTimeout(function() {
if(!pop || pop.closed || pop.closed == "undefined" || pop == "undefined" || parseInt(pop.innerWidth) == 0 || pop.document.documentElement.clientWidth != 150 || pop.document.documentElement.clientHeight != 150){
pop && pop.close();
alert("Popups must be enabled.");
}else{
alert("Popups is enabled.");
pop && pop.close();
}}, 1000);

看下面的问题

检测 Chrome 中被阻止的弹出窗口

如何检测弹出窗口是否在 chrome 中被阻止

在谷歌上它会更多地帮助你..

https://www.google.com/search?q=how+to+detect+a+blocked+popup+in+chrome

于 2013-05-14T09:07:52.467 回答
3

我发现使用 try-catch 更有效,如下所示:

var popup = window.open(winPath,winName,winFeature,true);
try {
    popup.focus();
} catch (e) {
    alert('popup blocked!');
}
于 2014-10-20T14:30:47.470 回答
0

我曾经使用这种方法从 js 打开窗口,并且没有被 Chrome 阻止。 http://en.nisi.ro/blog/development/javascript/open-new-window-window-open-seen-chrome-popup/

于 2013-12-31T08:23:29.327 回答
0

我知道这是“已解决”,但这个简单的代码对我在 Chrome 中检测“Better Popup Blocker”扩展很有用:

  if (!window.print) {
    //display message to disable popup blocker
  } else {
    window.print();
  }
}

奥卡姆剃刀!还是我错过了什么,不可能这么简单?

于 2013-06-30T21:22:51.970 回答
0

以下代码适用于 chrome、safari 和 firefox。我为此使用了jquery。

var popupWindow = window.open("http://www.google.com","directories=no,height=100,width=100");

$(document).ready(function(e) {
    detectPopup();
    function detectPopup() {
    if(!popupWindow) {
        alert("popup will be blocked");

    } else {
        alert("popup will be shown");
        window.open('','_self');
        window.close();
    } 
}
});
于 2015-09-17T04:49:08.257 回答