Robin Rodricks
Robin Rodricks

Reputation: 114046

Inline javascript with "<script>" string closes script tag by mistake

I'm inlining a large JS program, which includes a line of code like :

doc.write("<script>var app = \"" + _2d() + "\";</script>");

Unfortunately the browser (chrome) thinks the script in the string is the closing script tag, and actually takes everything after that like its HTML text.

How do I include such a string and escape it so it does not confuse the browser HTML parsing?

Upvotes: 9

Views: 5937

Answers (2)

Robin Rodricks
Robin Rodricks

Reputation: 114046

I solved it by splitting the script tag like this SO question recommends:

doc.write("<scr"+"ipt>var app = \"" + _2d() + "\";</scr"+"ipt>");

Upvotes: 4

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324730

You should always use <\/script> if you want to put </script> in a string in JS, because </script> marks the end of the tag no matter where it shows up.

Upvotes: 23

Related Questions