Lukas Z's Blog

Javascript Popup Windows With Rails

Javascript popups still have their uses. Here is a tiny snippet that one can use to create Javascript popup-windows in a Ruby on Rails application in an elegant fashion.

In your application.js (or whatever .js-file you prefer) include this:

$('a[data-popup]').live('click', function(e) { 
  window.open( $(this).attr('href'), "Popup", "height=600, width=600" ); 
  e.preventDefault(); 
});

Then, in your views add the data-popup-attribute to the link_to-helper:

<%= link_to( 'Open Popup-Window', popup_window_path, 'data-popup' => true ) %>

This is basically taken from [here](http://stackoverflow.com/a/8828841 “”). I have just added two extra parameters to window.open().

P.S.: You can follow me on Twitter.

Comments

Webmentions