Escaping HTML entities in JavaScript string literals within the [removed] block

前端 未结 5 717

On the one hand if I have

\';
console.log(s);

the browser will terminate the

5条回答
  •  攒了一身酷
    2020-12-03 05:24

    The following characters could interfere with an HTML or Javascript parser and should be escaped in string literals: <, >, ", ', \, and &.

    In a script block using the escape character, as you found out, works. The concatenation method (') can be hard to read.

    var s = 'Hello <\/script>';
    

    For inline Javascript in HTML, you can use entities:

    click me

    Demo: http://jsfiddle.net/ThinkingStiff/67RZH/

    The method that works in both

提交回复
热议问题