Add defer attribute to javascript_include_tag Rails

喜夏-厌秋 提交于 2019-12-20 11:03:46

问题


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>


回答1:


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

This will get you (in development):

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



回答2:


You can also do

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

which is more consistent with other switches.



来源:https://stackoverflow.com/questions/10982375/add-defer-attribute-to-javascript-include-tag-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!