umezo
umezo

Reputation: 1579

Should the javascript_include_tag always load last?

Railscast#369 explains how moving the javascript_include tag to the bottom of application.html.erb can shorten loading time, as it allows the page to load at the same time as the javascript.

From the simple example given, it seems like the javascript tag should ALWAYS be at the bottom of the page. However, the fact that the Rails default has it at the top implies (to me at least), that this may not always be true.

When would someone NOT move the javascript tag to the bottom of application.html.erb?

Upvotes: 1

Views: 340

Answers (1)

ScottJShea
ScottJShea

Reputation: 7111

If you call any javascript prior to it loading then you may run into issues. As an example in a view (blah.html.erb) file you might have:

<script>
    jquery('#element_id').append("<p>Error Message</p>");
</script>

Which would look for jQuery being loaded by the include tag. Not a good practice but sometimes occurs.

Also, you may use a js view file (blah.js.erb) which would look for jQuery before it is loaded as well.

Upvotes: 1

Related Questions