window.open on ajax response

4,870 views
Skip to first unread message

Hari

unread,
Jul 10, 2010, 8:49:11 AM7/10/10
to Prototype & script.aculo.us
Hi,

I have hyperlink. When a user clicks on it, i would make a ajax call
to get the location where a new browser window should be opened.

for example:
new Ajax.Request('someURL', {
onSuccess: function(response) {
var success = response.responseText;
success == "true" ? window.open("http://google.com") :
window.open("http://yahoo.com");
}
});

But problem is the window.open is called as part of onSuccess
callback. So the browser popup blocker blocks the call.

How can I get around this problem and want the popup to open.

Thanks,
Hari

ncubica

unread,
Jul 10, 2010, 10:29:45 PM7/10/10
to Prototype & script.aculo.us
I guess one easy option is to call a outside function like

new Ajax.Request('someURL', {
onSuccess: function(response) {
var success = response.responseText;
success == "true" ? openPopup("http://google.com") :
openPopup("http://yahoo.com");
}
});

function openPopup(site){
window.open(site);
}

mmm but what I don't know is if the browser is going to keep blocking
the new window... the only difference with this is the scope.

but that should be work...
Nahum

Hari

unread,
Jul 12, 2010, 12:31:01 AM7/12/10
to Prototype & script.aculo.us
But even this doesn't help since function call happens by a call back
and not user action. So the browser popup blocker click in.
One way i could think is make the Ajax call synchronous. But I think
its not a great idea to make the Ajax call synchronous...

Regards,
Hari

ColinFine

unread,
Jul 13, 2010, 5:15:13 AM7/13/10
to Prototype & script.aculo.us


On Jul 12, 5:31 am, Hari <hariharanwebm...@gmail.com> wrote:
> But even this doesn't help since function call happens by a call back
> and not user action. So the browser popup blocker click in.
> One way i could think is make the Ajax call synchronous. But I think
> its not a great idea to make the Ajax call synchronous...

I don't see how this would make any difference.

What you have not made clear are what are the circumstances in which
your browser blocks popups.
Without knowing that it is hard to suggest a solution.

It seems unlikely to me that it makes a difference whether the window
is opened from a callback or not, but I don't know.

Colin

Eric

unread,
Aug 4, 2010, 8:44:55 AM8/4/10
to Prototype & script.aculo.us
The browser probably blocks the popup because it is not opened by a
direct action of the user.

What you may try is:
- On the click event:
- Open the window on a blanc (or temporary) url
- Launch the Ajax request
- On the Ajax's onComplete:
- Change the url of the previously opened window

Eric

T.J. Crowder

unread,
Aug 4, 2010, 2:07:04 PM8/4/10
to Prototype & script.aculo.us
Hi,

> > But even this doesn't help since function call happens by a call back
> > and not user action. So the browser popup blocker click in.
> > One way i could think is make the Ajax call synchronous. But I think
> > its not a great idea to make the Ajax call synchronous...
>
> I don't see how this would make any difference.

It makes a difference because most browsers' popup blocking will allow
a window to be opened by code running during an event handler for a
user-initiated event (like a click), but block windows opened by code
running at other times (like an Ajax callback). So the synchronous
version would still be running during the event handler, so _may_
work. But then the OP would have to make a synchronous ajax call,
which is a Bad Thing. :-)

-- T.J.
Reply all
Reply to author
Forward
0 new messages