phil
phil

Reputation: 4908

rails 3 open new window

I want to have a link that does NOT redirect the main window, but opens a new window (popup, if you dare!).

Here is my code:

<%= link_to "Print Label", {:action => "show_label"}, :method => :get, 'data-popup' => true %>

With javascript:

<script type="text/javascript" charset="utf-8">
    $('a[data-popup]').live('click', function(e) { 
        window.open($(this)[0].href);
        e.preventDefault();
    });
</script>

What happens is that a new window opens but ALSO the main window moves on the link as well. It is like preventDefault is not stopping the action.

Any ideas?

Upvotes: 0

Views: 1089

Answers (2)

Scott Fister
Scott Fister

Reputation: 1283

I used

$(function () {
    $("a[data-popup]").bind('click', function(e) {
        window.open($(this)[0].href);
        e.preventDefault();
    });
});

and it works.

Upvotes: 0

Dan
Dan

Reputation: 300

I believe you need to add a return false; at the end of your javascript function. In order for the link not to execute its original functionality of reference the main page to its href.

Hope this helps.

Cheers,

Dan

Upvotes: 1

Related Questions