module ActionView::Helpers::AssetTagHelper::JavascriptTagHelpers
def javascript_include_tag(*sources)
The :recursive option is also available for caching:
# =>
javascript_include_tag "jquery", "cart", "checkout", :cache => "shop"
# assuming config.perform_caching is true
#
#
# =>
javascript_include_tag "jquery", "cart", "checkout", :cache => "shop"
# assuming config.perform_caching is false
# =>
javascript_include_tag :all, :cache => true
# assuming config.perform_caching is true
#
#
#
#
# =>
javascript_include_tag :all, :cache => true
# assuming config.perform_caching is false
==== Examples
production environment, but not for the development environment).
config.perform_caching is set to true (which is the case by default for the Rails
and can better be compressed by gzip (leading to faster transfers). Caching will only happen if
You can also cache multiple JavaScripts into one file, which requires less HTTP connections to download
== Caching multiple JavaScripts into one
javascript_include_tag :all, :recursive => true
explicitly set :recursive:
If you want Rails to search in all the subdirectories under public/javascripts, you should
included files.
Note that your defaults of choice will be included first, so they will be available to all subsequently
#
#
#
#
# =>
javascript_include_tag :all
You can also include all JavaScripts in the +javascripts+ directory using :all as the source:
* = The application.js file is only referenced if it exists
#
#
# =>
javascript_include_tag :defaults
# =>
javascript_include_tag "http://www.example.com/xmlhr.js"
# =>
javascript_include_tag "http://www.example.com/xmlhr"
#
# =>
javascript_include_tag "common.javascript", "/elsewhere/cools"
# =>
javascript_include_tag "xmlhr.js"
# =>
javascript_include_tag "xmlhr"
==== Examples
last argument.
You can modify the HTML attributes of the script tag by passing a hash as the
public/javascripts it will be included as well at the end.
When using :defaults, if an application.js file exists in
config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js)
and that can be overridden in config/application.rb:
expansion pass :defaults as source. By default, :defaults loads jQuery,
If the application is not using the asset pipeline, to include the default JavaScript
When passing paths, the ".js" extension is optional.
root. Relative paths are idiomatic, use absolute paths only when needed.
to public/javascripts, full paths are assumed to be relative to the document
Sources may be paths to JavaScript files. Relative paths are assumed to be relative
Returns an HTML script tag for each of the +sources+ provided.
def javascript_include_tag(*sources) @javascript_include ||= JavascriptIncludeTag.new(config, asset_paths) @javascript_include.include_tag(*sources) end
def javascript_path(source)
javascript_path "http://www.example.com/js/xmlhr" # => http://www.example.com/js/xmlhr
javascript_path "/dir/xmlhr" # => /dir/xmlhr.js
javascript_path "dir/xmlhr.js" # => /javascripts/dir/xmlhr.js
javascript_path "xmlhr" # => /javascripts/xmlhr.js
==== Examples
Used internally by javascript_include_tag to build the script path.
Full paths from the document root will be passed through.
If the +source+ filename has no extension, .js will be appended (except for explicit URIs)
Computes the path to a javascript asset in the public javascripts directory.
def javascript_path(source) asset_paths.compute_public_path(source, 'javascripts', :ext => 'js') end