mark smith
mark smith

Reputation: 20897

Javascript window.open is blocked by IE popup blocker

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

Upvotes: 12

Views: 63024

Answers (6)

forever hou
forever hou

Reputation: 26

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:

Upvotes: 0

xem tivi online
xem tivi online

Reputation: 11

please try code

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

Upvotes: 1

Grant Wagner
Grant Wagner

Reputation: 25931

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.

Upvotes: 5

ATorras
ATorras

Reputation: 4293

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?

Upvotes: 0

AnthonyWJones
AnthonyWJones

Reputation: 189439

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.

Upvotes: 13

nickytonline
nickytonline

Reputation: 6981

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

Upvotes: 3

Related Questions