tag in js string is being validated", "text": "<p>I have following page</p>\n\n<pre class="prettyprint"><code><html>\n<head>\n <script type="text/javascript" src="e01.js"></script>\n</head>\n<body>\n<script type="text/javascript">\nvar obj={someHTML: "<script>alert('a');</script>rest of the html", \n someOtherAttribute:"some value"};\n alert(obj.someHTML);\n</script>\n</body>\n</html>\n</code></pre>\n\n<p>in someHTML attribute of my object I have <code></script></code> tag in a string. but browser reads this as actual close tag and closes the script element. is there anything I am missing here? (tried it in ff and chrome) </p>", "answerCount": 2, "upvoteCount": 773, "dateCreated": "2026-03-11 13:35:05", "dateModified": "2026-03-14 03:40:56", "author": { "type": "Person", "name": "yilmazhuseyin" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>HTML is parsed before and independent from Javascript. The current browser behavior is that, once an open tag <code><script></code> is found, the browser will switch to "Script Data State" and interpret all following data as script until a <code></script></code> is found.</p>\n\n<p>Where the <code></script></code> is detected doesn't matter — inside a JS string, a JS comment, a CDATA section, or even HTML comment.</p>\n\n<p>You need to make the string does not look like <code></script></code> to the HTML parser. The simplest way is to write <code><\\/script></code> as in @Daniel's answer.</p>", "upvoteCount": 120, "url": "https://exchangetuts.com/index.php/why-script-tag-in-js-string-is-being-validated-1765519503010220#answer-1773495305324705", "dateCreated": "2026-03-13 13:40:56", "dateModified": "2026-03-14 03:40:56", "author": { "type": "Person", "name": "kennytm" } }, "suggestedAnswer": [ { "@type": "Answer", "text": "<p>You can either escape < and > by, respectively &lt; and &gt; or put the whole script in a CDATA section:</p>\n\n<pre class="prettyprint"><code><script type="text/javascript">\n<![CDATA[\nvar obj={someHTML: "<script>alert('a');</script>rest of the html", \n someOtherAttribute:"some value"};\n obj(some.pageButtonScript);\n]]>\n</script>\n</code></pre>", "upvoteCount": 23, "url": "https://exchangetuts.com/index.php/why-script-tag-in-js-string-is-being-validated-1765519503010220#answer-1773495305327900", "dateCreated": "2026-03-12 13:40:56", "dateModified": "2026-03-14 01:40:56", "author": { "type": "Person", "name": "Maurice Perry" } } ] } }