Add defer attribute to javascript_include_tag Rails

varatis picture varatis · Jun 11, 2012 · Viewed 16k times · Source

Is there some way to add the defer attribute easily using the javascript_include_tag helper in Rails?

I.e., is there some easy way to turn

<%= javascript_include_tag "blah.js" %>

into

<script defer src="blah.js"></script>

Answer

emrass picture emrass · Jun 11, 2012
<%= javascript_include_tag "blah.js", :defer => "defer" %>

This will get you (in development):

<script defer="defer" src="/assets/blah.js" type="text/javascript"></script>