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 andhost
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 totrue
, adds theasync
HTML attribute, allowing the script to be fetched in parallel to be parsed and evaluated as soon as possible. -
:defer
- When set totrue
, adds thedefer
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.
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 <script>
tag, please refer to the MDN docs.
Examples
javascript_include_tag "xmlhr"
# => <script src="/assets/xmlhr.debug-1284139606.js"></script>
javascript_include_tag "xmlhr", host: "localhost", protocol: "https"
# => <script src="https://localhost/assets/xmlhr.debug-1284139606.js"></script>
javascript_include_tag "template.jst", extname: false
# => <script src="/assets/template.debug-1284139606.jst"></script>
javascript_include_tag "xmlhr.js"
# => <script src="/assets/xmlhr.debug-1284139606.js"></script>
javascript_include_tag "common.javascript", "/elsewhere/cools"
# => <script src="/assets/common.javascript.debug-1284139606.js"></script>
# <script src="/elsewhere/cools.debug-1284139606.js"></script>
javascript_include_tag "http://www.example.com/xmlhr"
# => <script src="http://www.example.com/xmlhr"></script>
javascript_include_tag "http://www.example.com/xmlhr.js"
# => <script src="http://www.example.com/xmlhr.js"></script>
javascript_include_tag "http://www.example.com/xmlhr.js", nonce: true
# => <script src="http://www.example.com/xmlhr.js" nonce="..."></script>
javascript_include_tag "http://www.example.com/xmlhr.js", async: true
# => <script src="http://www.example.com/xmlhr.js" async="async"></script>
javascript_include_tag "http://www.example.com/xmlhr.js", defer: true
# => <script src="http://www.example.com/xmlhr.js" defer="defer"></script>
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'actionview/lib/action_view/helpers/asset_tag_helper.rb', line 111 def javascript_include_tag(*sources) = sources..stringify_keys = .extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys preload_links = [] nopush = ["nopush"].nil? ? true : .delete("nopush") crossorigin = .delete("crossorigin") crossorigin = "anonymous" if crossorigin == true integrity = ["integrity"] rel = ["type"] == "module" ? "modulepreload" : "preload" = sources.uniq.map { |source| href = path_to_javascript(source, ) if preload_links_header && !["defer"] && href.present? && !href.start_with?("data:") preload_link = "<#{href}>; rel=#{rel}; as=script" preload_link += "; crossorigin=#{crossorigin}" unless crossorigin.nil? preload_link += "; integrity=#{integrity}" unless integrity.nil? preload_link += "; nopush" if nopush preload_links << preload_link end = { "src" => href, "crossorigin" => crossorigin }.merge!() if ["nonce"] == true ["nonce"] = content_security_policy_nonce end content_tag("script", "", ) }.join("\n").html_safe if preload_links_header send_preload_links_header(preload_links) end end |