Escape single quotes, htmlspecialchar ” < > &, and fix line endings.
Escapes text strings for echoing in JS. It is intended to be used for inline JS (in a tag attribute, for example onclick=”…”). Note that the strings have to be in single quotes. The filter ‘js_escape’ is also applied here.
Signature
esc_js( $text )
- text
- (string) The text to be escaped.
Return
(string) Escaped text.
Source
function esc_js( $text ) {
$safe_text = wp_check_invalid_utf8( $text );
$safe_text = _wp_specialchars( $safe_text, ENT_COMPAT );
$safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) );
$safe_text = str_replace( "\r", '', $safe_text );
$safe_text = str_replace( "\n", '\\n', addslashes( $safe_text ) );
return apply_filters( 'js_escape', $safe_text, $text );
}
WP Trac GitHub Bitbucket
Link here
-
URL
http://queryposts.com/function/esc_js/ -
HTML
<a href='http://queryposts.com/function/esc_js/'>esc_js()</a> -
Markdown
[esc_js()](http://queryposts.com/function/esc_js/) -
BBCode
[url=http://queryposts.com/function/esc_js/]esc_js()[/url]