2013-05-14 93 views
15

找遍了許多問題堆棧溢出,並可能是重複的這裏Detect Popup如何在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的更好的解決方案?

+1

爲什麼羽絨服選民......有問題的問題是什麼?我無法理解 – 2013-05-20 06:53:57

+3

有些人認爲瀏覽器行爲是一種不好的做法。我個人覺得這很有用。 – BradGreens 2014-04-08 19:12:12

回答

19

最後,成功通過COMBIN從#1的成員荷蘭國際集團不同的答案
此代碼爲我工作&在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

謝謝你的作品:) – ranggadablues 2013-05-20 02:51:49

+1

如果有人遇到這個,你的彈出式檢測仍然無法正常工作,請確保你沒有將它連接到可信任事件:http:// www .w3.org/TR/DOM-Level-3-Events /#trusted-events – 2014-11-10 19:02:36

+0

在沒有else子句的情況下完美工作,在Firefox上,else子句實際上會啓動一個空選項卡。 – 2014-12-03 23:38:06

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

+1

對於第二個鏈接+1,它幫助我 – 2013-05-14 11:05:07

0

測試我知道這是「解決」,但這個簡單的代碼爲我工作檢測鉻「更好的彈出窗口攔截」擴展名:

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

奧卡姆的剃刀!或者我錯過了一些東西,它不可能是這麼簡單?

2

我發現它更有效使用的try-catch如下:

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

下面的代碼工作在鉻,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(); 
    } 
} 
}); 
相關問題