setec setec - 3 days ago 4
Javascript Question

Why js can't understand string '</script>'?

I have a simple javascript (jsFiddle):

alert('</script>');


Browser fails to understand it.

This is console output:

Uncaught SyntaxError: Unexpected token ILLEGAL


But this script works (jsFiddle):

alert('</scriptt>');//shows alert text '</scriptt>'


Is it some kind of browser bug or normal
ECMAScript
behaviour?

(browser is Chrome)

Answer

Because it is considered as:

<script>
alert('
</script>
');

which is a SyntaxError

You can use

alert( '<\/script>\n');
Comments