Forum Moderators: open

Message Too Old, No Replies

New window blocked through window.confirm

Called from <body onunload="...">

         

sagentry

5:20 pm on Mar 5, 2010 (gmt 0)

10+ Year Member



My problem is simple.

I created a method called popup() in JS and call it via onunload. Example:


//Webpage
<body onunload="javascript: popup()">
...

//JS page
exited = 0;
function popup() {
var x=window.confirm("Would you like to fill out a quick survey?")
if (x){
window.open('survey_comments.php','Survey/Comments','height=300,width=800, scrollbars=yes, resizable=yes, left=30, top=30');
}
}


Essentially, when someone exits the page it asks if they would like to fill out a survey. If yes: open new window.

I am confused as to why there are no problems when implemented as a link (<a href="www.mysite.com" onclick="javascript:popup()">...</a>) but not through a confirm box. I thought if a new window was created via a link/button internally it would not block the newly created window. Thanks for any help.

whoisgregg

6:48 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does the confirm come up at all?

One thing I notice is the "javascript:" you have prepended there does not need to be there and may be causing problems. The only time people use that is when they try to put javascript in an href attribute and that's not even valid usage.

sagentry

6:56 pm on Mar 5, 2010 (gmt 0)

10+ Year Member



True. I actually didn't have it there at first but put it in later when I got to that point in testing where you just start guessing =) But yes, the confirm page does come up with and without "javascript:". It just always gets blocked unlike in a href.

Fotiman

7:03 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



According to MSDN [msdn.microsoft.com]:
If you call window.open from this event, the Pop-up Blocker feature in Microsoft Internet Explorer 6 prevents the pop-up window from appearing.


And Mozilla Developer Center [developer.mozilla.org]:
Browsers equipped with pop-up window blockers will ignore all window.open() method calls in onunload event handler functions.


So it sounds to me like this is a built in security feature. For example, to prevent malicious sites from forcing open new windows every time the user tried to navigate away.

sagentry

7:10 pm on Mar 5, 2010 (gmt 0)

10+ Year Member



I was afraid of that. I'll try and find an alternative if I can. Thanks for the information and timely response.