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 -%>
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/action_view/helpers/javascript_helper.rb', line 168 def javascript_tag( = nil, = {}, &block) content = if block_given? = if .is_a?(Hash) capture(&block) else end tag = content_tag(:script, javascript_cdata_section(content), .merge(:type => Mime::JS)) if block_called_from_erb?(block) concat(tag) else tag end end |