Method: ActionView::Helpers::AssetTagHelper#javascript_include_tag

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

#javascript_include_tag(*sources) ⇒ Object

Returns an HTML script tag for each of the sources provided.

Sources may be paths to JavaScript files. Relative paths are assumed to be relative to assets/javascripts, full paths are assumed to be relative to the document root. Relative paths are idiomatic, use absolute paths only when needed.

When passing paths, the ".js" extension is optional. If you do not want ".js" appended to the path extname: false can be set on the options.

You can modify the HTML attributes of the script tag by passing a hash as the last argument.

When the Asset Pipeline is enabled, you can pass the name of your manifest as source, and include other JavaScript or CoffeeScript files inside the manifest.

If the server supports HTTP Early Hints, and the defer option is not enabled, Rails will push a 103 Early Hints response that links to the assets.

Options

When the last parameter is a hash you can add HTML attributes using that parameter. This includes but is not limited to the following options:

  • :extname - Append an extension to the generated URL unless the extension already exists. This only applies for relative URLs.
  • :protocol - Sets the protocol of the generated URL. This option only applies when a relative URL and host options are provided.
  • :host - When a relative URL is provided the host is added to the that path.
  • :skip_pipeline - This option is used to bypass the asset pipeline when it is set to true.
  • :nonce - When set to true, adds an automatic nonce value if you have Content Security Policy enabled.
  • :async - When set to true, adds the async HTML attribute, allowing the script to be fetched in parallel to be parsed and evaluated as soon as possible.
  • :defer - When set to true, adds the defer HTML attribute, which indicates to the browser that the script is meant to be executed after the document has been parsed. Additionally, prevents sending the Preload Links header.
  • :nopush - Specify if the use of server push is not desired for the script. Defaults to true.

Any other specified options will be treated as HTML attributes for the script tag.

For more information regarding how the :async and :defer options affect the