Javascript window.open is blocked by IE popup blocker

北城余情 提交于 2019-11-27 02:44:17

问题


Can anyone help, I have a popup that is being blocked. It is a popup that is created because somebody has clicked on a Print picture on my site.

I thought IE is not supposed to block these when the popup came via an onclick?

Can anyone help? The child1 variable is always returned as NULL if popup blocker enabled...

Maybe the problem is that the onclick event then passes control to a new function which loads a html file and does child.document.write

Here is my simple code..

var width = 800;
var height = 600;
var left = parseInt((screen.availWidth / 2) - (width / 2));
var top = parseInt((screen.availHeight / 2) - (height / 2));
var windowFeatures = "width=" + width + ",height=" + height 
   + ",menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes,left=" 
   + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;      

child1 = window.open("about:blank", "subWind", windowFeatures);

回答1:


The problem will be that open will only return a reference to the window if you are navigating to somewhere within your current host. You are navigating to about:blank which is not within your host.

Try adding a blank.htm file to your site and open that instead. I'm still not sure that document.write will be allowed the document won't be open for write, you may be able to manipulate the DOM of the existing blank document though.




回答2:


The Internet Explorer pop-up blocker, when set to Medium filter level, will not block windows opened by JavaScript if they have been initiated by a user action. The following works fine in IE 6, 7 and 8:

<script type="text/javascript">
function openWin() {
    var width = 800;
    var height = 600;
    var left = Math.floor((screen.availWidth - width) / 2);
    var top = Math.floor((screen.availHeight - height) / 2);
    var windowFeatures = "width=" + width + ",height=" + height +
            ",menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes," +
            "left=" + left + ",top=" + top +
            "screenX=" + left + ",screenY=" + top;
    child1 = window.open("about:blank", "subWind", windowFeatures);
    writeTo(child1);
}
function writeTo(w) {
    w.document.write('Test');
}
</script>
<a href="#" onclick="openWin();return false;">Test</a>

Note that using document.write into the newly opened window does not work in some web browsers. Also note that this may trigger a pop-up blocker in other browsers, even if it works as shown in Internet Explorer.

I have seen where invoking JavaScript from an onclick event can, in some cases, cause the pop-up blocker to trigger. It seems to have something to do with how far window.open() is from the onclick event. Too many levels of functions calling functions before you call window.open(). Without seeing your exact implementation, I can't tell you whether this is the problem you are having or not.




回答3:


It blocks it because it's not an anchor with a target="_blank". You're creating the popup programatically.

Just do this after the code example you've provided.

if (!child1) {
      alert('You have a popup blocker enabled. Please allow popups for www.yourSite.com');
}



回答4:


please try code

var newWin = window.open(...);
if (newWin && newWin.top) {
    // popup has opened
} else {
    // popup has been blocked
}



回答5:


I suggest you to make a dummy [form] with a target="_blank" instead of a window.open().

I hope it works.

Regards.

PD: I suppose adding your site to "trusted sites" is not an option, right?




回答6:


i have a idea in ie 10/11 :

let win = window.open();
setTimeout(function(){
    win.customParam = 'xxxx';
});

in win ,you can get customParam after window onload with a timer or loop:



来源:https://stackoverflow.com/questions/888964/javascript-window-open-is-blocked-by-ie-popup-blocker

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