\" 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><script type="text/javascript">\nvar a = new Array("</script>");\nalert(a);\n</script>\n</code></pre>\n\n<p>...doesn't work as expected (by me at least). The string <code>"</script>"</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>"</script>"</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></script></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><script type="text/javascript">\nvar a = new Array("<\\/script>");\nalert(a);\n</script>\n</code></pre>\n\n<p>Here's an updated fiddle.</p>", "upvoteCount": 177, "url": "https://exchangetuts.com/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" } } } }