public
Last active

Rails Helper to call javascript asynchronous

  • Download Gist
async_helper.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
module AsyncHelper
 
def javascript_async(*args)
content_tag :script, type: "text/javascript" do
"(function() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = '#{j javascript_path(*args)}';
var other = document.getElementsByTagName('script')[0];
other.parentNode.insertBefore(script, other);
})();".html_safe
end
end
 
end

Thanks you!
I used to do it in javascript way, but making it a helper is even better :D

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.