How do I detect whether popups are blocked in chrome

為{幸葍}努か 提交于 2019-12-23 02:34:42

问题


I'm trying to to load facebook auth page for users to connect their fb accounts with my site. The problem is it's a different domain. Code is as below

var left = (screen.width/2)-(550/2);
    var top = (screen.height/2)-(250/2);
    fbPopUp = window.open(authorizeURL,'fbWindow','menubar=0,status=0,titlebar=0,toolbar=0,resizable=1,width=400,height=275, top='+top+' , left='+left);

How do I find out whether this window actually opened or not? I tried solution of Detect blocked popup in Chrome


回答1:


Basically there's a bug in Chrome. Although it hides the popup, it still executes and you still get the window object back - so regular checks don't work.

Here's the solution that worked for me:

var popup = window.open(url);

if (popup) {
  popup.onload = function () {
    console.log(popup.innerHeight > 0 ? 'open' : 'blocked');
  }
} else {
  console.log('blocked');
}

Working example here: http://jsbin.com/uticev/3/




回答2:


I was searching and searching, and finally found this, so I had to share, becuase like other people said it is broken in newer versions of chrome. So the fixed solution is this:

var popUp = window.open( url );
setTimeout( function() {
   if ( popUp.outerHeight === 0 ) {   
      alert('blocked'); 
   }
}, 25);



回答3:


just modified Remy answer. This works for me.

        var win = window.open("", 'child','width=10,height=10,status=no,resizable=no');
        var objwin = new RegExp('object','gi');
        var isblock = false;

        if(objwin.test(String(win))) {
            if(typeof win.outerHeight ==="undefined" || parseInt(win.outerHeight)<10){
                isblock = true;
            }
            win.close();
        }else{
               isblock = true;
        }

        if(isblock){
          // do something here
        }



回答4:


Simple:

var popup = window.open(host);
popup.onload = function (){
    if (!popup.innerHeight > 0){
        popup.close();
        // Popup blocked
    } else {
        // Popup enabled
    }
}



回答5:


This solution works well for me:

loginWindow = window.open(url, '_blank', options);

setTimeout(function () {
    if (!loginWindow || loginWindow.closed || typeof loginWindow.closed == 'undefined' || parseInt(loginWindow.outerWidth) == 0) {
        alert('Turn off pop-up blocker and try again.');
    }
    else {
        loginWindow.focus();
    }
}, 500);


来源:https://stackoverflow.com/questions/8802409/how-do-i-detect-whether-popups-are-blocked-in-chrome

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!