jQuery 如何在 Chrome 中检测弹出窗口阻止程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16538991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:19:45  来源:igfitidea点击:

How do I detect popup blocker in Chrome?

javascriptjquerygoogle-chromepopup-blocker

提问by Surendra Jnawali

I have searched many issue in stack overflow and might be duplicatehere Detect Popup

我在堆栈溢出中搜索了许多问题,可能在这里 重复检测弹出窗口

But not helped for mewhile testing in Chrome(tested v26.0.1410.64)
Following Approach Worked in IE and Firefoxbut not in Chrome

但是not helped for meChrome 中测试时(tested 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();
} 

Any better solution that works for Chrome also?

有没有更好的解决方案也适用于 Chrome?

回答by Surendra Jnawali

Finally, it success by combining different answer from Stackoverflow's member
This code worked for me & tested in IE, Chrome & Firefox

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

回答by Kishan Patel

Try Below..!!

试试下面..!!

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

Look on below question

看下面的问题

Detect blocked popup in Chrome

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

How do I detect whether popups are blocked in chrome

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

On Google It will more help you..

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

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

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

回答by cw24

I found it much more effective to use try-catch as follows:

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

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

回答by user5345000

The below code works in chrome,safari and firefox. I have used jquery for this.

以下代码适用于 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();
    } 
}
});

回答by Adam

I know this is "resolved", but this simple code worked for me detecting "Better Popup Blocker" extension in Chrome:

我知道这是“已解决”,但是这个简单的代码对我在 Chrome 中检测“更好的弹出窗口阻止程序”扩展有用:

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

Ockham's razor! Or am I missing something and it couldn't possibly be this simple?

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

回答by Cooper M.

I had used this method to open windows from js and not beeing blocked by Chrome. http://en.nisi.ro/blog/development/javascript/open-new-window-window-open-seen-chrome-popup/

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