如何在Chrome中检测弹出窗口拦截器?

时间:2013-05-14 09:01:03

标签: javascript jquery google-chrome popup-blocker

我在堆栈溢出中搜索了很多问题,可能重复这里Detect Popup

但在 Chrome (测试版本号v26.0.1410.64)中进行测试时not helped for me 遵循方法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?

6 个答案:

答案 0 :(得分:19)

最后,它通过结合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);

答案 1 :(得分:2)

尝试以下.. !!

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

看下面的问题

Detect blocked popup in Chrome

How do I detect whether popups are blocked in chrome

在谷歌上它会对你有所帮助..

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

答案 2 :(得分:2)

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

var popup = window.open(winPath,winName,winFeature,true);
try {
    popup.focus();
} catch (e) {
    alert('popup blocked!');
}

答案 3 :(得分:0)

我知道这是“已解决”,但这个简单的代码可以帮助我检测Chrome中的“更好的弹出窗口拦截器”扩展程序:

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

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

答案 4 :(得分:0)

我曾使用此方法从js打开窗口而不被Chrome阻止。 http://en.nisi.ro/blog/development/javascript/open-new-window-window-open-seen-chrome-popup/

答案 5 :(得分: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();
    } 
}
});