Frage

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)

War es hilfreich?

Lösung

Because it is considered as:

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

which is a SyntaxError

You can use

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

Andere Tipps

The HTML parser does not understand JavaScript und thus looks for something which closes the tag which is </script>. If you need '</script>' as string in JavaScript simply use '</s'+'cript>'.

JavaScript isself does not have such a problem, using var x = '</script>'; in nodejs is no problem. The HTML parser is.

This is Javascript embedded in HTML script tags, right?

Then the HTML parser terminates your script in the middle.

Put the Javascript into its own file or break up the string literal. Maybe a CDATA section also works.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top