instance method
escape_javascript
Ruby on Rails 5.2.8.1
Since v2.2.3Signature
escape_javascript(javascript)
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' %>');
Parameters
-
javascriptreq
Source
# File actionview/lib/action_view/helpers/javascript_helper.rb, line 29
def escape_javascript(javascript)
if javascript
result = javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"']|[`]|[$])/u) { |match| JS_ESCAPE_MAP[match] }
javascript.html_safe? ? result.html_safe : result
else
""
end
end
Defined in actionview/lib/action_view/helpers/javascript_helper.rb line 29
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::JavaScriptHelper