将“<”转换为“\<”和将“>”转换为“>”的主要目的是避免以下内联脚本:
<script>
var foo = "</script><script>alert('bug');</script><script>"; // the value of foo is generated from server
</script>
foo 的字符串值是从服务器端生成的。所以,我们打算把“<”改成“\<”,把“>”改成“>”。 (我知道有人认为“>”应该转义为“>”,但在这种情况下不考虑。)
所以,预期的结果是:
<script>
var foo = "\</script\>\<script\>alert('bug');\</script\>\<script\>"; // the value of foo is generated from server
</script>
对于IE7/8和Firefox,HTML渲染引擎不会将javascript字符串中的\<script\>当成<script>标签,JavaScript引擎仍然把它作为字符串“<script>”。但是,我不确定是否所有浏览器都以这种方式处理“>”和“\<”。这种标准适用于所有浏览器吗?
最佳答案
不,最好的办法是使用 > <分别为大于号和小于号。
所以你会想要这样的东西:
var foo = "</script><script>alert('bug');</script><script>";
关于javascript - 所有浏览器引擎都将 "\<"视为 "<"并将 "\>"视为 ">"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/805143/