javascript_tag(content_or_options_with_block = nil, html_options = {}, &block) public

Returns a JavaScript tag with the content inside. Example:

javascript_tag "alert('All is good')"


<script type="text/javascript">
alert('All is good')

html_options may be a hash of attributes for the <script> tag. Example:

javascript_tag "alert('All is good')", :defer => 'defer'
# => <script defer="defer" type="text/javascript">alert('All is good')</script>

Instead of passing the content as an argument, you can also use a block in which case, you pass your html_options as the first parameter.

<%= javascript_tag :defer => 'defer' do -%>
  alert('All is good')
<% end -%>
Show source
Register or log in to add new notes.