Method: ActionView::Helpers::AssetTagHelper#javascript_include_tag

Defined in:
lib/action_view/helpers/asset_tag_helper.rb

#javascript_include_tag(*sources) ⇒ Object

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" # =>

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 config.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 config.perform_caching is false => ...

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

javascript_include_tag "prototype", "cart", "checkout", :cache => "shop" # when config.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 config.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


345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/action_view/helpers/asset_tag_helper.rb', line 345

def javascript_include_tag(*sources)
  options = sources.extract_options!.stringify_keys
  concat  = options.delete("concat")
  cache   = concat || options.delete("cache")
  recursive = options.delete("recursive")

  if concat || (config.perform_caching && cache)
    joined_javascript_name = (cache == true ? "all" : cache) + ".js"
    joined_javascript_path = File.join(joined_javascript_name[/^#{File::SEPARATOR}/] ? config.assets_dir : config.javascripts_dir, joined_javascript_name)

    unless config.perform_caching && File.exists?(joined_javascript_path)
      write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources, recursive))
    end
    javascript_src_tag(joined_javascript_name, options)
  else
    sources = expand_javascript_sources(sources, recursive)
    ensure_javascript_sources!(sources) if cache
    sources.collect { |source| javascript_src_tag(source, options) }.join("\n").html_safe
  end
end