views:

9667

answers:

7

Hi there,

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);
+2  A: 

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');
}
nickyt
+6  A: 

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.

AnthonyWJones
Yes thank you this was it, basically i need to load a file from my current web an d not BLANK ... so i created a empty html file .. loaded it in and used windows.write to write my html.. thank you and thanks for everyone elses comments
mark smith
A: 

Hi,

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?

ATorras
+1  A: 

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.

Grant Wagner
+1  A: 
<object width="550" height="400">
<param name="movie" value="somefilename.swf">
<embed src="somefilename.swf" width="550" height="400">
</embed>
</object>
A: 

please try code var newWin = window.open(...); if (newWin && newWin.top) { // popup has opened } else { // popup has been blocked } mua ban may tinh

xem tivi online
A: 

I know this was this was some time ago now - but I just cant get this working. Using Grants suggestion I can get it to work without pop up blocker but it's not ideal. I'm struggling with Mark & Anthony's suggestion of creating a file on the host and using windows.write to write the html. I'm so close but am stuck. Please could anyone provide an example for graphic deisgner struggling with code (again:)) Many Thanks :)

Daynie
If you can't get a solution to an old solution to work you probably need more help than is available in this question. Considering opening a new question, link back to this question and explain carefully what went wrong. Asking your question as a new answer to an old solved question isn't going to get help.
Yi Jiang