是否所有浏览器引擎都将"\ <"视为"<",将"\>"视为">"?

Mor*_*eng 1 html javascript browser escaping

使"<"到"\ <"和">"到">"的要点是避免使用下面的内联脚本:

<script>
   var foo = "</script><script>alert('bug');</script><script>"; // the value of foo is generated from server
</script>
Run Code Online (Sandbox Code Playgroud)

foo的字符串值是从服务器端生成的.因此,我们计划将"<"更改为"\ <",将">"更改为">".(我知道有论点">"应该转义为">",但在这种情况下不考虑.)

所以,预期的结果是:

<script>
   var foo = "\</script\>\<script\>alert('bug');\</script\>\<script\>"; // the value of foo is generated from server
</script>
Run Code Online (Sandbox Code Playgroud)

对于IE7/8和Firefox,HTML呈现引擎不会将javascript字符串中的\ <script \>视为<script>标记,而JavaScript引擎仍将其视为字符串"<script>".但是,我不确定所有浏览器是否都以这种方式处理">"和"\ <".这是所有浏览器的标准吗?

Jus*_*ner 5

不,你最好的选择是使用> 和< 分别大于和小于标志.

所以你想要这样的东西:

var foo = "&lt;/script&gt;&lt;script&gt;alert('bug');&lt;/script&gt;&lt;script&gt;";
Run Code Online (Sandbox Code Playgroud)