admin管理员组文章数量:1192929
What is the real reason that we must escape a forward slash in a JavaScript string, and also why must we escape string/no string in XHTML. A lot of tutorials just brush over this issue.
What is the real reason that we must escape a forward slash in a JavaScript string, and also why must we escape string/no string in XHTML. A lot of tutorials just brush over this issue.
Share Improve this question edited Sep 9, 2015 at 18:28 Jonathan Leffler 753k145 gold badges946 silver badges1.3k bronze badges asked May 24, 2011 at 23:04 rubixibucrubixibuc 7,39721 gold badges64 silver badges106 bronze badges3 Answers
Reset to default 15What is the real reason that we must escape a forward slash in a JavaScript string
In an HTML 4 document, the sequence </
inside an element defined as containing CDATA (such as script) is an end tag and will end the element (with an error if it is not </script>
.
As far as JS is concerned /
and \/
are identical inside a string. As far as HTML is concerned </
starts an end tag but <\/
does not.
, and also why must we escape string/no string in XHTML.
XHTML doesn't provide a method of specifying that an element intrinsically contains CDATA, so you need to explicitly handle characters which would otherwise have special meaning (<
, &
, etc). Wrapping the contents of the element with CDATA markers is the easiest way to achieve this.
You don't need to escape /
in a JavaScript string, just \
, because if you don't, then the string yes\no
will inadvertently be transformed into yes<newline>o
. Escaping the \
will prevent that.
Also, if you don't escape &
in a URL, then whatever comes after it will be considered a new parameter. For example, a=Q&A
will mean "the parameter a
has the value "Q
" and there's also a parameter A
" instead of "the parameter a
has the value "Q&A
"". The correct way of escaping that would be a=Q%26A
.
The slash is needed to prevent browsers, particularly older ones, to erroneously interpret the forward slash as a closing JavaScript marker.
本文标签: JavaScript and forward slashes in stringsStack Overflow
版权声明:本文标题:JavaScript and forward slashes in strings - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738473021a2088699.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论