2

I'm getting the following error: SyntaxError: unterminated string literal. Seems like there is a problem with the first data += command. What is wrong with it? I don't break any line.

        // Data
        var data = "<!doctype html><html><head>";
        data += "<script type='text/javascript' src='test1.js'></script>";
        data += "</head><body>TEST</body></html>";
user3631654
  • 1,735
  • 6
  • 22
  • 38

1 Answers1

3

You can't include the sequence of characters </script> in a script block. The HTML parser assumes that it ends the block.

The typical way of handling that is something like

    data += "<script type='text/javascript' src='test1.js'></" + "script>";

The HTML parser does not understand JavaScript syntax. When it sees <script>, it just does a blind search through subsequent content looking for </script>.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • You're right, adding it like you described it solved the problem. But now the parser executes the JavaScript even if it is inside a string, e.g. `data += 'console.log("Inline");<'+'/script>';`. "Inline" will appear in the console – user3631654 Dec 10 '14 at 21:41
  • @user3631654 That's how ` – Pointy Dec 10 '14 at 21:45
  • But it is inside a string! that makes no sense. I also splitted the ` – user3631654 Dec 10 '14 at 21:54
  • Maybe you know also a solution to this problem: http://stackoverflow.com/questions/27411562/ie-executes-inline-scripts-before-external-in-dynamic-iframe-content – user3631654 Dec 10 '14 at 21:55