", "text": "<p>I need a little assistance. I have to create a javascript string that contains more javascript that is then written to a div tag in the parent window. The code is as follows:</p>\n\n<pre class="prettyprint"><code><script language="javascript" type="text/javascript">\nvar jstr2 = '';\njstr2 += '<script language="javascript">\\n';\njstr2 += 'function doPagingProducts(str) {\\n';\njstr2 += 'document.frmPagingProducts.PG.value = str\\;\\n';\njstr2 += 'document.frmPagingProducts.submit()\\;\\n';\njstr2 += 'return false\\;\\n';\njstr2 += '}\\n';\njstr2 += '</script>\\n';\njstr2 += '\\n';\n</script>\n</code></pre>\n\n<p>However the closing script tag in the created string actually close the javascript and I get errors such as:</p>\n\n<pre class="prettyprint"><code>Error: unterminated string literal\nLine: 135, Column: 9 ( The </script> line before the end of the string.)\nSource Code:\njstr2 += '\n</code></pre>\n\n<p>Is there any way I can prevent this issue..?</p>\n\n<p>Many thanks for all your help.</p>\n\n<p>Best Regards,\nPaul</p>\n\n<hr>\n<p><strong>edit</strong> I finally solved this problem by extracting the final <code></script></code> from the javascript string. I added a end tag where the script shows. Its messy, but it works. Many thanks for all your comments. </p>", "answerCount": 3, "upvoteCount": 828, "dateCreated": "2010-12-10 00:54:59", "dateModified": "2022-10-11 02:39:43", "author": { "type": "Person", "name": "neojakey" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>The <em>SCRIPT</em> tag is content agnostic, so the parser just keeps running through the content until it finds a <em>/SCRIPT</em> sequence. When it does, it passes the content it's found to the JS environment for evaluation. That gives you your unterminated literal error because the sent content ends where your <em>/SCRIPT</em> begins. (There is no terminating quote mark to be found for the JS parser).</p>\n\n<p>Escaping the slash with backslash </p>\n\n<pre class="prettyprint"><code>jstr2 += "<\\/script>";\n</code></pre>\n\n<p>or some other work-around hack breaks the trigger point in the sequence here and solves this problem (but still leaves you with some very dubious code). </p>", "upvoteCount": 195, "url": "https://exchangetuts.com/javascript-variable-that-contains-script-1640466723856255#answer-1652555775190205", "dateCreated": "2022-09-28 03:34:04", "dateModified": "2022-10-11 01:39:43", "author": { "type": "Person", "name": "Michiel Kalkman" } }, "suggestedAnswer": [ { "@type": "Answer", "text": "<p>Write it as:</p>\n\n<pre class="prettyprint"><code>jstr2 += '<\\/script>\\n';\n</code></pre>", "upvoteCount": 6, "url": "https://exchangetuts.com/javascript-variable-that-contains-script-1640466723856255#answer-1665491983761016", "dateCreated": "2022-10-09 12:39:43", "dateModified": "2022-10-11 02:39:43", "author": { "type": "Person", "name": "Ryan Tenney" } }, { "@type": "Answer", "text": "<p>You have to split the string:</p>\n\n<pre class="prettyprint"><code>jstr2 += '<' + '/script>\\n';\n</code></pre>\n\n<p>It's also better to comment out everything inside the script:</p>\n\n<pre class="prettyprint"><code><script type="text/javascript">\n<!--//\n // your code here\n//-->\n</script>\n</code></pre>\n\n<p>Or in HTML:</p>\n\n<pre class="prettyprint"><code><script type="text/javascript">\n//<![CDATA[\n // your code here\n//]]>\n</script>\n</code></pre>\n\n<p>Or in XHTML: </p>\n\n<ul>\n<li>same as HTML but #PCDATA instead of CDATA.</li>\n</ul>", "upvoteCount": 4, "url": "https://exchangetuts.com/javascript-variable-that-contains-script-1640466723856255#answer-1665491983765084", "dateCreated": "2022-10-09 12:39:43", "dateModified": "2022-10-11 00:39:43", "author": { "type": "Person", "name": "vol7ron" } } ] } }