Javascript window.open is blocked by IE popup blocker

[亡魂溺海] 提交于 2019-11-28 09:13:09

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.

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.

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');
}
xem tivi online

please try code

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

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?

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:

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