Method: ActionView::Helpers::JavaScriptHelper#javascript_tag
- Defined in:
- lib/action_view/helpers/javascript_helper.rb
#javascript_tag(content_or_options_with_block = nil, html_options = {}, &block) ⇒ Object
Returns a JavaScript tag with the content inside. Example:
javascript_tag "alert('All is good')"
Returns:
<script type="text/javascript">
//<![CDATA[
alert('All is good')
//]]>
</script>
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 -%>
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/action_view/helpers/javascript_helper.rb', line 50 def javascript_tag( = nil, = {}, &block) content = if block_given? = if .is_a?(Hash) capture(&block) else end content_tag(:script, javascript_cdata_section(content), .merge(:type => Mime::JS)) end |