Flowdock
javascript_include_tag(*sources) public

Returns an html script tag for each of the sources provided. You can pass in the filename (.js extension is optional) of javascript files that exist in your public/javascripts directory for inclusion into the current page or you can pass the full path relative to your document root. To include the Prototype and Scriptaculous javascript libraries in your application, pass :defaults as the source. When using :defaults, if an application.js file exists in your public javascripts directory, it will be included as well. You can modify the html attributes of the script tag by passing a hash as the last argument.

Examples

  javascript_include_tag "xmlhr" # =>
    <script type="text/javascript" src="/javascripts/xmlhr.js"></script>

  javascript_include_tag "xmlhr.js" # =>
    <script type="text/javascript" src="/javascripts/xmlhr.js"></script>

  javascript_include_tag "common.javascript", "/elsewhere/cools" # =>
    <script type="text/javascript" src="/javascripts/common.javascript"></script>
    <script type="text/javascript" src="/elsewhere/cools.js"></script>

  javascript_include_tag "http://www.railsapplication.com/xmlhr" # =>
    <script type="text/javascript" src="http://www.railsapplication.com/xmlhr.js"></script>

  javascript_include_tag "http://www.railsapplication.com/xmlhr.js" # =>
    <script type="text/javascript" src="http://www.railsapplication.com/xmlhr.js"></script>

  javascript_include_tag :defaults # =>
    <script type="text/javascript" src="/javascripts/prototype.js"></script>
    <script type="text/javascript" src="/javascripts/effects.js"></script>
    ...
    <script type="text/javascript" src="/javascripts/application.js"></script>
  • = The application.js file is only referenced if it exists

Though it’s not really recommended practice, if you need to extend the default JavaScript set for any reason (e.g., you’re going to be using a certain .js file in every action), then take a look at the register_javascript_include_default method.

You can also include all javascripts in the javascripts directory using :all as the source:

  javascript_include_tag :all # =>
    <script type="text/javascript" src="/javascripts/prototype.js"></script>
    <script type="text/javascript" src="/javascripts/effects.js"></script>
    ...
    <script type="text/javascript" src="/javascripts/application.js"></script>
    <script type="text/javascript" src="/javascripts/shop.js"></script>
    <script type="text/javascript" src="/javascripts/checkout.js"></script>

Note that the default javascript files will be included first. So Prototype and Scriptaculous are available to all subsequently included files.

If you want Rails to search in all the subdirectories under javascripts, you should explicitly set :recursive:

  javascript_include_tag :all, :recursive => true

Caching multiple javascripts into one

You can also cache multiple javascripts into one file, which requires less HTTP connections to download and can better be compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching is set to true (which is the case by default for the Rails production environment, but not for the development environment).

Examples

  javascript_include_tag :all, :cache => true # when ActionController::Base.perform_caching is false =>
    <script type="text/javascript" src="/javascripts/prototype.js"></script>
    <script type="text/javascript" src="/javascripts/effects.js"></script>
    ...
    <script type="text/javascript" src="/javascripts/application.js"></script>
    <script type="text/javascript" src="/javascripts/shop.js"></script>
    <script type="text/javascript" src="/javascripts/checkout.js"></script>

  javascript_include_tag :all, :cache => true # when ActionController::Base.perform_caching is true =>
    <script type="text/javascript" src="/javascripts/all.js"></script>

  javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when ActionController::Base.perform_caching is false =>
    <script type="text/javascript" src="/javascripts/prototype.js"></script>
    <script type="text/javascript" src="/javascripts/cart.js"></script>
    <script type="text/javascript" src="/javascripts/checkout.js"></script>

  javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when ActionController::Base.perform_caching is true =>
    <script type="text/javascript" src="/javascripts/shop.js"></script>

The :recursive option is also available for caching:

  javascript_include_tag :all, :cache => true, :recursive => true
Show source
Register or log in to add new notes.
July 20, 2010
3 thanks

When scripts don't end in .js

For example, Google Custom Search’s URL is http://www.google.com/jsapi

It’s an ugly hack, but works:

= javascript_include_tag('http://www.google.com/jsapi').sub('.js', '')
December 2, 2009
2 thanks

IE GOTCHA - multiple javascript_include_tags with cache => true

If you have multiple lines of javascript_include_tag ‘jsfile’, :cache => true, IE does not load them all (though it seems Firefox and Safari do). And the error won’t show up until you’re in production (since that’s only when caching kicks in.)

You should include them all on one line:

javascript_include_tag 'file1.js', 'file2.js', 'file3.js', :cache => 'myfiles'
July 10, 2010
2 thanks

Re: IE GOTCHA

@insane-dreamer

That has nothing to do with IE. When you specify :cache => true you are saying that the files referenced should be saved to a file called all.js. When the script encounters the next line, it will overwrite the same file with the new contents.

Caching is not compressing, it doesn’t make sense to do with individual files, but it can make sense some times. I someone wants to do it, just specify a name for the cached file:

javascript_include_tag 'layout', 'typography', :cache => 'base'
javascript_include_tag 'admin/layout', 'admin/extras', :cache => 'admin'