9

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?

3

2 Answers 2

23

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This! The simple and elegant solution.
4

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

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

1 Comment

Don't do that. It is slightly less efficient and significantly uglier then Kolink's solution. (And there is absolutely no need to break up the start tag)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.