I have a page with the following code:
<script>
window.str = '<script></script>';
</script>
This results in an error:
Uncaught SyntaxError: Unexpected token ILLEGAL
What's going on? Entering the same code into the console runs without issue. Here is a jsbin demonstrating the issue
[javascript] "</script>"</script>tag inside the string closes the first<script>tag, leaving you with invalid JavaScript. HTML doesn't know about JavaScript, it only sees the closing tag. It works in the console because the code is not in a HTML context.