123456789_123456789_123456789_123456789_123456789_

Module: ActionView::Helpers::JavaScriptHelper

Overview

Action View JavaScript Helpers

Constant Summary

  • JS_ESCAPE_MAP =
    # File 'actionview/lib/action_view/helpers/javascript_helper.rb', line 11
    {
      "\\"    => "\\\\",
      "</"    => '<\/',
      "\r\n"  => '\n',
      "\n"    => '\n',
      "\r"    => '\n',
      '"'     => '\\"',
      "'"     => "\\'",
      "`"     => "\\`",
      "$"     => "\\$",
      (+"\342\200\250").force_encoding(Encoding::UTF_8).encode!.freeze => "&#x2028;",
      (+"\342\200\251").force_encoding(Encoding::UTF_8).encode!.freeze => "&#x2029;"
    }.freeze

Class Attribute Summary

Instance Attribute Summary

Instance Method Summary

Class Attribute Details

.auto_include_nonce (rw) Also known as: #auto_include_nonce

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/javascript_helper.rb', line 9

mattr_accessor :auto_include_nonce

Instance Attribute Details

#auto_include_nonce (rw)

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/javascript_helper.rb', line 9

mattr_accessor :auto_include_nonce

Instance Method Details

#escape_javascript(javascript) Also known as: #j

Escapes carriage returns and single and double quotes for JavaScript segments.

Also available through the alias j(). This is particularly helpful in JavaScript responses, like:

$('some_element').replaceWith('<%= j render 'some/element_template' %>');
[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/javascript_helper.rb', line 33

def escape_javascript(javascript)
  javascript = javascript.to_s
  if javascript.empty?
    result = ""
  else
    result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"']|[`]|[$])/u, JS_ESCAPE_MAP)
  end
  javascript.html_safe? ? result.html_safe : result
end

#j(javascript)

Alias for #escape_javascript.

[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/javascript_helper.rb', line 43

alias_method :j, :escape_javascript

#javascript_cdata_section(content)

This method is for internal use only.
[ GitHub ]

  
# File 'actionview/lib/action_view/helpers/javascript_helper.rb', line 113

def javascript_cdata_section(content) # :nodoc:
  "\n//#{cdata_section("\n#{content}\n//")}\n".html_safe
end

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

Returns a JavaScript tag with the content inside. Example:

javascript_tag "alert('All is good')"

Returns:

<script>
//<![CDATA[
alert('All is good')
//]]>
</script>

html_options may be a hash of attributes for the \