public Method

AssetTagHelper.javascript_include_tag(*sources)

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 src="/javascripts/xmlhr.js" type="text/javascript"></script>

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

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

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

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

javascript_include_tag :defaults # =>
  <script src="/javascripts/prototype.js" type="text/javascript"></script>
  <script src="/javascripts/effects.js" type="text/javascript"></script>
  ...
  <script src="/javascripts/application.js" type="text/javascript"></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 src="/javascripts/prototype.js" type="text/javascript"></script>
  <script src="/javascripts/effects.js" type="text/javascript"></script>
  ...
  <script src="/javascripts/application.js" type="text/javascript"></script>
  <script src="/javascripts/shop.js" type="text/javascript"></script>
  <script src="/javascripts/checkout.js" type="text/javascript"></script>

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

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 src="/javascripts/prototype.js" type="text/javascript"></script>
  <script src="/javascripts/effects.js" type="text/javascript"></script>
  ...
  <script src="/javascripts/application.js" type="text/javascript"></script>
  <script src="/javascripts/shop.js" type="text/javascript"></script>
  <script src="/javascripts/checkout.js" type="text/javascript"></script>

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

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

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

Source Code

# File action_view/helpers/asset_tag_helper.rb, line 237
def javascript_include_tag(*sources)
  options = sources.extract_options!.stringify_keys
  cache   = options.delete("cache")

  if ActionController::Base.perform_caching && cache
    joined_javascript_name = (cache == true ? "all" : cache) + ".js"
    joined_javascript_path = File.join(JAVASCRIPTS_DIR, joined_javascript_name)

    write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources))
    javascript_src_tag(joined_javascript_name, options)
  else
    expand_javascript_sources(sources).collect { |source| javascript_src_tag(source, options) }.join("\n")
  end
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.