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?
In the same way, there are some tags which cannot be self-closed. And <script> tag is one of them. Basically, the concept of self-closing tags is an XML concept. You can use them in XHTML if the document is served with an XML content-type but not if it is served as text/html.
That's because SCRIPT TAG is not a VOID ELEMENT. In an HTML Document - VOID ELEMENTS do not need a "closing tag" at all!
The best practice is to put JavaScript <script> tags just before the closing </body> tag rather than in the <head> section of your HTML. The reason for this is that HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body.
JavaScript in body or head: Scripts can be placed inside the body or the head section of an HTML page or inside both head and body.
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.
I solved it by splitting the script tag like this SO question recommends:
doc.write("<scr"+"ipt>var app = \"" + _2d() + "\";</scr"+"ipt>");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With