esc_js()
esc_js( string $text ) 转义单引号、htmlspecialchar“&”,并修复…
esc_js( string $text )
转义单引号、htmlspecialchar“&”,并修复行尾。
Escape single quotes, htmlspecialchar ” &, and fix line endings.
转义单引号、htmlspecialchar“&”,并修复行尾。
Escape single quotes, htmlspecialchar ” &, and fix line endings.
目录锚点:#说明#参数#返回#源码#笔记
说明(Description)
在JS中转义用于回显的文本字符串。它打算用于内联JS(在标记属性中,例如onclick=“…”)。注意字符串必须用单引号。这里还应用了“js_escape”过滤器。
参数(Parameters)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| $text | (string) | 必需 | 要转义的文本。 |
返回(Return)
(string)转义文本。
源码(Source)
/**
* 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.
*
* @since 2.8.0
*
* @param string $text The text to be escaped.
* @return string Escaped text.
*/
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( "
", '', $safe_text );
$safe_text = str_replace( "
", 'n', addslashes( $safe_text ) );
/**
* Filter a string cleaned and escaped for output in JavaScript.
*
* Text passed to esc_js() is stripped of invalid or special characters,
* and properly slashed for output.
*
* @since 2.0.6
*
* @param string $safe_text The text after it has been escaped.
* @param string $text The text prior to being escaped.
*/
return apply_filters( 'js_escape', $safe_text, $text );
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 2.8.0 | wp-includes/formatting.php:4428 | 18 | 4 |
笔记(Notes)
我再也看不到使用esc_js()的价值了。如果确实需要执行内联脚本属性,可以考虑使用wp_json_encode()和esc_attr()执行以下示例,这看起来更易于读取和维护:
例子
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!