\" in an array is interpreted, is there a workaround? [duplicate]", "text": "<div>\n \n\n </div>\n\n<p>This :</p>\n\n<pre class="prettyprint"><code>&lt;script type="text/javascript"&gt;\nvar a = new Array("&lt;/script&gt;");\nalert(a);\n&lt;/script&gt;\n</code></pre>\n\n<p>...doesn't work as expected (by me at least). The string <code>"&lt;/script&gt;"</code> is interpreted as the end of the script even though it's in quotes. Please see these jsfiddles :</p>\n\n<p>http://jsfiddle.net/RYbTS/</p>\n\n<p>http://jsfiddle.net/PB73z/</p>\n\n<p>The first declares the first element of the array in the normal way while the second declares it in a compact form. In both cases, browsers (Chrome, FF, IE) stop the script at <code>"&lt;/script&gt;"</code> and never get to the alert.</p>\n\n<p>It behaves the same way whether using quotes or double quotes.</p>\n\n<p>Is it expected ? Is there a workaround?</p>\n\n<p>Edit: thanks all, I'll escape the backslash. Sorry I couldn't accept everyone's answer, I accepted the first one, but thanks to everyone.</p>\n\n<p>The link provided by Juhana explains this behavior:</p>\n\n<blockquote>\n <p>all the HTML is parsed before the text nodes in the element are passed to the JS engine, <code>&lt;/script&gt;</code> gets no special treatment for being inside a JS string literal</p>\n</blockquote>", "answerCount": 1, "upvoteCount": 616, "dateCreated": "2025-11-24 05:25:07", "dateModified": "2025-11-25 17:26:51", "author": { "type": "Person", "name": "Michel MM" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>You could escape the <code>/</code> character:</p>\n\n<pre class="prettyprint"><code>&lt;script type="text/javascript"&gt;\nvar a = new Array("&lt;\\/script&gt;");\nalert(a);\n&lt;/script&gt;\n</code></pre>\n\n<p>Here's an updated fiddle.</p>", "upvoteCount": 177, "url": "https://exchangetuts.com/index.php/the-string-script-in-an-array-is-interpreted-is-there-a-workaround-duplicate-1763652303481442#answer-1764134707202966", "dateCreated": "2025-11-24 05:26:51", "dateModified": "2025-11-25 17:26:51", "author": { "type": "Person", "name": "James Allardice" } } } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The string "</script>" in an array is interpreted, is there a workaround? [duplicate]

Tags:

javascript

This :

<script type="text/javascript">
var a = new Array("</script>");
alert(a);
</script>

...doesn't work as expected (by me at least). The string "</script>" is interpreted as the end of the script even though it's in quotes. Please see these jsfiddles :

http://jsfiddle.net/RYbTS/

http://jsfiddle.net/PB73z/

The first declares the first element of the array in the normal way while the second declares it in a compact form. In both cases, browsers (Chrome, FF, IE) stop the script at "</script>" and never get to the alert.

It behaves the same way whether using quotes or double quotes.

Is it expected ? Is there a workaround?

Edit: thanks all, I'll escape the backslash. Sorry I couldn't accept everyone's answer, I accepted the first one, but thanks to everyone.

The link provided by Juhana explains this behavior:

all the HTML is parsed before the text nodes in the element are passed to the JS engine, </script> gets no special treatment for being inside a JS string literal

like image 616
Michel MM Avatar asked Nov 24 '25 05:11

Michel MM


1 Answers

You could escape the / character:

<script type="text/javascript">
var a = new Array("<\/script>");
alert(a);
</script>

Here's an updated fiddle.

like image 177
James Allardice Avatar answered Nov 25 '25 17:11

James Allardice



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!