in a comment break the parser? [duplicate]", "text": "<p>Why does adding <code>&lt;/script&gt;</code> in a comment break the parser? Is this a bug or is there something in the documentation I've overlooked?</p> <p>I've tested this in Chrome, Firefox, Opera, Internet Explorer and they all produce the same result.</p> <p><strong>Single-line comment:</strong></p> <p></p> <div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false"> <div class="snippet-code"> <pre class="prettyprint snippet-code-js lang-js prettyprint-override"><code>function Foo(){ // &lt;/script&gt; alert("bar"); }; Foo();</code></pre> </div> </div> <p><strong>Multi-line comment:</strong></p> <p></p> <div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false"> <div class="snippet-code"> <pre class="prettyprint snippet-code-js lang-js prettyprint-override"><code>function Foo(){ /* &lt;/script&gt; */ alert("bar"); }; Foo();</code></pre> </div> </div>", "answerCount": 1, "upvoteCount": 419, "dateCreated": "2015-09-30 07:25:01", "dateModified": "2022-09-30 18:43:19", "author": { "type": "Person", "name": "Bjørn-Roger Kringsjå" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>This happens because HTML parser as defined by W3C is totally separated from the JavaScript parser. After the <code>&lt;script&gt;</code> tag it looks for the closing <code>&lt;/script&gt;</code>, regardless that it's inside comments or strings, because it treats JS code as normal text.</p>", "upvoteCount": 184, "url": "https://exchangetuts.com/why-does-adding-script-in-a-comment-break-the-parser-duplicate-1639543148183288#answer-1646917068848639", "dateCreated": "2022-09-30 06:43:19", "dateModified": "2022-09-30 18:43:19", "author": { "type": "Person", "name": "mck89" } } } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does adding </script> in a comment break the parser? [duplicate]

Why does adding </script> in a comment break the parser? Is this a bug or is there something in the documentation I've overlooked?

I've tested this in Chrome, Firefox, Opera, Internet Explorer and they all produce the same result.

Single-line comment:

function Foo(){    // </script>    alert("bar");  };    Foo();

Multi-line comment:

function Foo(){    /*        </script>    */    alert("bar");  };    Foo();
like image 419
Bjørn-Roger Kringsjå Avatar asked Sep 30 '15 07:09

Bjørn-Roger Kringsjå


1 Answers

This happens because HTML parser as defined by W3C is totally separated from the JavaScript parser. After the <script> tag it looks for the closing </script>, regardless that it's inside comments or strings, because it treats JS code as normal text.

like image 184
mck89 Avatar answered Sep 30 '22 18:09

mck89