" closes the script tag by mistake I embed a large JS program that includes a line of code, for example:...">

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

I embed a large JS program that includes a line of code, for example:

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

Unfortunately, the browser (chrome) considers the script in the line to be the closing tag of the script and actually accepts everything after that as its HTML text.

How to include such a line and avoid it so that it doesn’t confuse HTML browser parsing?

+7
source share
2 answers

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

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

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 appears.

+20
source

All Articles