/**
* Determine whether the browser to block popup window or not
* @callback call back function
* for example: callback(blocked)
* blocked:true - blocked
* blocked:false - not blocked
*/
function hasPopupWinBlocked(callback) {
var testWin = window.open('', 'TestBlock', 'top='+window.innerHeight+',left=1,width=1,height=1,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
var isChrome = false;
var result = false;
try {
if (testWin == null) { //IE, Firefox and most other browsers
result = true;
} else if (typeof testWin == 'undefined') { //Safari
result = true;
} else { //chrome
isChrome = true;
testWin.onunload = function(){
if (testWin.innerWidth == window.innerWidth || testWin.innerWidth == 0) {
result = true;
}
if (callback) {
callback(result);
}
}
}
} catch (err) {
if (console) {
console.warn("Could not access popup window", err);
}
}
if (callback && !isChrome) {
callback(result);
}
if (testWin) {
testWin.close();
}
}
调用例子:
function test() {
hasPopupWinBlocked(function(blocked){
if (blocked) {
//remind something if popup window was blocked
}
});
}