javascript_include_tag
javascript_include_tag(*sources)
public
Returns a script include tag per source given as argument. Examples:
javascript_include_tag "xmlhr" # => <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 :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> *see below
If there’s an application.js file in your public/javascripts directory, javascript_include_tag :defaults will automatically include it. This file facilitates the inclusion of small snippets of JavaScript code, along the lines of controllers/application.rb and helpers/application_helper.rb.
When scripts don't end in .js
For example, Google Custom Search’s URL is http://www.google.com/jsapi
It’s an ugly hack, but works:
= javascript_include_tag('http://www.google.com/jsapi').sub('.js', '')
IE GOTCHA - multiple javascript_include_tags with cache => true
If you have multiple lines of javascript_include_tag ‘jsfile’, :cache => true, IE does not load them all (though it seems Firefox and Safari do). And the error won’t show up until you’re in production (since that’s only when caching kicks in.)
You should include them all on one line:
javascript_include_tag 'file1.js', 'file2.js', 'file3.js', :cache => 'myfiles'
Re: IE GOTCHA
@insane-dreamer
That has nothing to do with IE. When you specify :cache => true you are saying that the files referenced should be saved to a file called all.js. When the script encounters the next line, it will overwrite the same file with the new contents.
Caching is not compressing, it doesn’t make sense to do with individual files, but it can make sense some times. I someone wants to do it, just specify a name for the cached file:
javascript_include_tag 'layout', 'typography', :cache => 'base' javascript_include_tag 'admin/layout', 'admin/extras', :cache => 'admin'