ActionView::Helpers::JavaScriptHelper

escape_javascript

API docs (Rails 8.1.4.1) ↗

Alias: j

def escape_javascript(javascript)

Description

Returns the string with characters escaped so that it can go inside a JavaScript string.

Arguments

"He said \"hi\" and \u0027bye\u0027\n</script>"
Adds \ before \, ", ', `, $ and newlines so that the string can go inside a JavaScript string (a newline, including \r\n and \r, becomes \n). Also converts </ to <\/. Given an html_safe string, it returns an html_safe string; otherwise the returned string is not HTML-escaped, so in the HTML section <%= %> escapes characters such as " (to &quot;).

ERB

Changes from the previous output: Added Removed Changed


          

HTML