簡體   English   中英

如何在 Chrome 中檢測彈出窗口阻止程序?

[英]How do I detect popup blocker in Chrome?

我在堆棧溢出中搜索了許多問題,可能在這里重復檢測彈出窗口

但在Chrome 中測試時not helped for me (已測試 v26.0.1410.64)
以下方法適用Worked in IE and Firefoxnot 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?

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

試試下面..!!

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

我發現使用 try-catch 更有效,如下所示:

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

我知道這是“已解決”,但是這個簡單的代碼對我在 Chrome 中檢測“更好的彈出窗口阻止程序”擴展有用:

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

奧卡姆剃刀! 或者我錯過了什么,它不可能這么簡單?

我曾使用這種方法從 js 打開窗口,而沒有被 Chrome 阻止。 http://en.nisi.ro/blog/development/javascript/open-new-window-window-open-seen-chrome-popup/

以下代碼適用於 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();
    } 
}
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM