tag in js string is being validated", "text": "<p>I have following page</p>\n\n<pre class="prettyprint"><code>&lt;html&gt;\n&lt;head&gt;\n &lt;script type="text/javascript" src="e01.js"&gt;&lt;/script&gt;\n&lt;/head&gt;\n&lt;body&gt;\n&lt;script type="text/javascript"&gt;\nvar obj={someHTML: "&lt;script&gt;alert('a');&lt;/script&gt;rest of the html", \n someOtherAttribute:"some value"};\n alert(obj.someHTML);\n&lt;/script&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n\n<p>in someHTML attribute of my object I have <code>&lt;/script&gt;</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>&lt;script&gt;</code> is found, the browser will switch to "Script Data State" and interpret all following data as script until a <code>&lt;/script&gt;</code> is found.</p>\n\n<p>Where the <code>&lt;/script&gt;</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>&lt;/script&gt;</code> to the HTML parser. The simplest way is to write <code>&lt;\\/script&gt;</code> as in @Daniel's answer.</p>", "upvoteCount": 120, "url": "https://exchangetuts.com/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 &lt; and &gt; by, respectively &amp;lt; and &amp;gt; or put the whole script in a CDATA section:</p>\n\n<pre class="prettyprint"><code>&lt;script type="text/javascript"&gt;\n&lt;![CDATA[\nvar obj={someHTML: "&lt;script&gt;alert('a');&lt;/script&gt;rest of the html", \n someOtherAttribute:"some value"};\n obj(some.pageButtonScript);\n]]&gt;\n&lt;/script&gt;\n</code></pre>", "upvoteCount": 23, "url": "https://exchangetuts.com/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" } } ] } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why </script> tag in js string is being validated

Tags:

javascript

I have following page

<html>
<head>
    <script type="text/javascript" src="e01.js"></script>
</head>
<body>
<script type="text/javascript">
var obj={someHTML: "<script>alert('a');</script>rest of the html",  
               someOtherAttribute:"some value"};
    alert(obj.someHTML);
</script>
</body>
</html>

in someHTML attribute of my object I have </script> 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)

like image 773
yilmazhuseyin Avatar asked Mar 11 '26 13:03

yilmazhuseyin


2 Answers

HTML is parsed before and independent from Javascript. The current browser behavior is that, once an open tag <script> is found, the browser will switch to "Script Data State" and interpret all following data as script until a </script> is found.

Where the </script> is detected doesn't matter — inside a JS string, a JS comment, a CDATA section, or even HTML comment.

You need to make the string does not look like </script> to the HTML parser. The simplest way is to write <\/script> as in @Daniel's answer.

like image 120
kennytm Avatar answered Mar 14 '26 03:03

kennytm


You can either escape < and > by, respectively &lt; and &gt; or put the whole script in a CDATA section:

<script type="text/javascript">
<![CDATA[
var obj={someHTML: "<script>alert('a');</script>rest of the html",  
               someOtherAttribute:"some value"};
    obj(some.pageButtonScript);
]]>
</script>
like image 23
Maurice Perry Avatar answered Mar 14 '26 01:03

Maurice Perry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!