Method: ActionView::Helpers::JavaScriptHelper#escape_javascript

Defined in:
actionpack/lib/action_view/helpers/javascript_helper.rb

#escape_javascript(javascript) ⇒ Object Also known as: j

Escape carrier 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' %>');


19
20
21
22
23
24
25
26
# File 'actionpack/lib/action_view/helpers/javascript_helper.rb', line 19

def escape_javascript(javascript)
  if javascript
    result = javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) {|match| JS_ESCAPE_MAP[match] }
    javascript.html_safe? ? result.html_safe : result
  else
    ''
  end
end