41

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>

varatis
  • 14,494
  • 23
  • 71
  • 114

2 Answers2

52
<%= javascript_include_tag "blah.js", :defer => "defer" %>

This will get you (in development):

<script defer="defer" src="/assets/blah.js" type="text/javascript"></script>
emrass
  • 6,253
  • 3
  • 35
  • 57
32

You can also do

<%= javascript_include_tag "blah.js", defer: true %>

which is more consistent with other switches.

Obromios
  • 15,408
  • 15
  • 72
  • 127