How to open a new tab when a button is clicked in Rails?

I have a button that moves the user to another page. (This should be a no link button). I want to open the page in a new tab (or even in a new window!). Should I use JavaScript? or is there another way! I would be grateful for your help.

Greetings.

+7
source share
5 answers

Expand answers suggesting to use `target =" _ blank "in Rails ...

If you use the button_to , this is automatically button_to in the Rails form. This is the form tag that needs this attribute. To get it, pass it as a value in the html hash settings, for example:

 button_to t(:translated_text), foo_path(foo.id), class: 'btn btn-primary', form: {target: '_blank'} 

class: and form: are elements of the html_options hash. The form: parameter is special because it tells Rails that you want to pass the HTML parameter to the form tag, instead of the input tag of the button itself.

+14
source

This is more of an html question. If you want to use buttons instead of links, you can provide the goal attribute to the form as described here: http://www.w3schools.com/tags/att_form_target.asp

0
source
 $('#btn').click(function(){ window.open('pageName'); 

})

0
source

target="_blank" is what you want, but it only works with forms and links.

You can easily create a link to look like a button with css, which would be the cleanest way to achieve what you are after.

If your button is not in the form, you can simply add target="_blank" to your form, and it should work.

0
source

With javascript, you select all your buttons with the href attribute

 $("button[href]").click(function() { window.open($(this).attr('href')); }); 

This actually opens a new window, but I think when the browser forces it to open in a new tab, it will open in a new tab.

Hooray!

0
source

All Articles