18

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

But not helped for me while testing in Chrome (tested v26.0.1410.64)
Following Approach Worked in IE and Firefox but 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?

Community
  • 1
  • 1
Surendra Jnawali
  • 3,190
  • 5
  • 28
  • 44

6 Answers6

21

Finally, it success by combining different answer from Stackoverflow's member
This code worked for me & tested in 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);
Surendra Jnawali
  • 3,190
  • 5
  • 28
  • 44
  • 1
    If anyone comes across this and your pop up detection is not working still, make sure you are not attaching it to a trusted event: http://www.w3.org/TR/DOM-Level-3-Events/#trusted-events – David Nguyen Nov 10 '14 at 19:02
  • works perfectly without the else clause, on firefox the else clause will actually launch an empty tab. – jeantimex Dec 03 '14 at 23:38
  • @SuYong, In else clause you can show your custom page... I did display blank page for my purpose.... – Surendra Jnawali Feb 09 '15 at 11:36
  • 1
    This code doesn't currently work in Chrome on Windows. You need to increase the timeout, otherwise it will always say the popup blocker is blocking even when it isn't. I've changed the timeout to 250ms and it seems to work fine in all cases. This appears to be a recent change in Windows Chrome, as it was working before. – CpnCrunch Feb 17 '16 at 00:28
  • It gives me an error in the console (and breaks the page): "(index):2210 Uncaught ReferenceError: winPath is not defined". the URL: http://dev.magazinuldecase.ro/adauga-anunt-vanzare-inchiriere/ – Razvan Zamfir Sep 09 '16 at 12:28
  • @RazvanZamfir you need to set winPath, OP had done so but hadn't shown it being set e.g. `var winPath = document.URL` – Michael Millar Feb 08 '18 at 16:15
3

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

How do I detect whether popups are blocked in chrome

On Google It will more help you..

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

Community
  • 1
  • 1
Kishan Patel
  • 1,358
  • 10
  • 24
3

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

var popup = window.open(winPath,winName,winFeature,true);
try {
    popup.focus();
} catch (e) {
    alert('popup blocked!');
}
cw24
  • 1,613
  • 2
  • 22
  • 31
0

I know this is "resolved", but this simple code worked for me detecting "Better Popup Blocker" extension in 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?

Adam
  • 613
  • 7
  • 16
0

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/

Cooper M.
  • 67
  • 1
  • 1
  • 1
    answer with just outgoing links are not recommended. Please post a minimum gist as to what is in the link. – Rohan Kandwal Dec 31 '13 at 08:47
  • I wouldn't worry too much, the method on the linked page does not work at all - as soon as you set the myPopup variable, Chrome blocks it! – MC9000 May 11 '15 at 18:17
0

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

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