Page << "window.open('http://www.google.com');" <<--- HAS NO EFFECT

Hi all,

In an AJAX action, I want to open a new window.
problem: the code below does not work : I see the 2 alerts, but
‘window.open’ does not open a new window.

controller :

def test1
render(:update){ |page|
page << “alert(‘before’)”
page << “window.open(‘http://www.google.com’);” <<— HAS NO
EFFECT
page << “alert(‘after’)”
}
end

view :

<%= link_to_remote “open a new window with RJS”, :url => {:action =>
‘test1’} %>

I’m stuck here. Any idea what’s happening?

Alain

Go get Firebug: http://www.getfirebug.com/

If you’re still stuck, post what the resulting error is.

Jason

On 10/23/07, Alain R. [email protected] wrote:

Hi all,

In an AJAX action, I want to open a new window.
problem: the code below does not work : I see the 2 alerts, but
‘window.open’ does not open a new window.

Ditto on Firebug.

Probably a popup blocker is preventing the window from opening. I’m
not sure how to work around that. Try disabling your popup blocker to
verify that is the problem.

Probably a popup blocker is preventing the window from opening.

That was it, not that it makes sense : the Firefox popup-blocker will

  • allow (open)

    <%= link_to “open a new window”, root_path, :target => ‘child2’ %>

  • but not

    render(:update){ |page|
    page << “window.open(’#{root_path}’);”
    }

2� :
I fear a double “NO”, but I’ll ask anyway : is there

  • another javascript way to open a window that would go through the
    popup blocker, or
  • a way to detect (even indirectly) that the client’s browser has a
    popup blocker on?

Alain

A quick Google resulted in this:

http://www.visitor-stats.com/articles/detect-popup-blocker.php

Looks like it might work to detect the blocker.

Note that their javascript is sort of… lame. The trick of opening a
window and seeing if it is set makes sense, but they always close the
window’s object, which will likely fail. You probably want to move
that into the ‘if the window opened’ code only.

If you are doing this trick, I’d highly recommend only doing it once
then storing it on the session or something. Having a small window
pop up on every page hit will likely drive your users insane, and
having the pop-up window thing appear constantly will likely do the
same.

–Michael

http://www.visitor-stats.com/articles/detect-popup-blocker.php

Thanks Michael.

There’s a much simpler way actually :

page << “if (confirm(‘#{msg}’) ) {”
page << " new_win=window.open(‘#{root_path}’);"
page << " if (new_win==null) {alert(‘Please disable your popup
blocker’);}"
page << “}”

Alain

Instead of a real browser window, try a javascript one, like this
http://www.stickmanlabs.com/lightwindow

have you tried:

page.call ‘window.open’, ‘http://www.google.com