Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 248
mark smith Avatar asked May 20 '09 16:05

mark smith


1 Answers

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.

like image 130
Grant Wagner Avatar answered Oct 21 '22 21:10

Grant Wagner