Putting Javascript at the end of the file using Rails 3.1 asset pipeline

别来无恙 提交于 2019-12-08 04:07:12

问题


Sometimes in heavy client side Javascript we put the tags at the end of the HTML file so that the content is displayed first while Javascript is loaded afterwards.

Is it possible to do this using Rails 3.1 assets pipeline?

EDIT:

<html>
<head>
   <%= javascript_include_tag "application" %>
</head>

<body>
<!-- all the page content goes here -->


<!-- we include these at the bottom to ensure the html loads first and the javascript is loaded afterwards. How can we achieve this through rails asset pipelining? -->

<script src="/some_other_assets/first_file.js"></script>
<script src="/some_other_assets/second_file.js"></script>
<script src="/some_other_assets/third_file.js"></script>

</body>

</html>

回答1:


Yes, you can do this with Rails 3.1 — it doesn't make any difference (as far as Rails is concerned) where you put your javascript_include_tag lines in your view/layout.




回答2:


Not sure if you got the answer for this, but you can include multiple manifest files and set up which files you want at the bottom of the page.

In other words, I could create a footer.js manifest file, tell it to include the scripts you want, and then at the bottom of your view template, include it the same way you do your application.js file with the

<%= javascript_include_tag("footer") %>

tag.

You can read more here-

http://coderberry.me/blog/2012/04/24/asset-pipeline-for-dummies/



来源:https://stackoverflow.com/questions/8038484/putting-javascript-at-the-end-of-the-file-using-rails-3-1-asset-pipeline

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