javascript 将 defer 属性添加到 javascript_include_tag Rails

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10982375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:42:10  来源:igfitidea点击:

Add defer attribute to javascript_include_tag Rails

javascriptruby-on-railsruby-on-rails-3ruby-on-rails-3.1deferred-execution

提问by varatis

Is there some way to add the deferattribute easily using the javascript_include_taghelper in Rails?

有什么方法defer可以使用javascript_include_tagRails 中的帮助程序轻松添加属性吗?

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

即,是否有一些简单的方法可以转动

<%= javascript_include_tag "blah.js" %>

<%= javascript_include_tag "blah.js" %>

into

进入

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

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

回答by emrass

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

This will get you (in development):

这将使您(在开发中):

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

回答by Obromios

You can also do

你也可以这样做

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

which is more consistent with other switches.

这与其他开关更一致。