", "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>&lt;script language="javascript" type="text/javascript"&gt;\nvar jstr2 = '';\njstr2 += '&lt;script language="javascript"&gt;\\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 += '&lt;/script&gt;\\n';\njstr2 += '\\n';\n&lt;/script&gt;\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 &lt;/script&gt; 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>&lt;/script&gt;</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 += "&lt;\\/script&gt;";\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 += '&lt;\\/script&gt;\\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 += '&lt;' + '/script&gt;\\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>&lt;script type="text/javascript"&gt;\n&lt;!--//\n // your code here\n//--&gt;\n&lt;/script&gt;\n</code></pre>\n\n<p>Or in HTML:</p>\n\n<pre class="prettyprint"><code>&lt;script type="text/javascript"&gt;\n//&lt;![CDATA[\n // your code here\n//]]&gt;\n&lt;/script&gt;\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" } } ] } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript variable that contains </script>

Tags:

javascript

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:

<script language="javascript" type="text/javascript">
var jstr2 = '';
jstr2 += '<script language="javascript">\n';
jstr2 += 'function doPagingProducts(str) {\n';
jstr2 += 'document.frmPagingProducts.PG.value = str\;\n';
jstr2 += 'document.frmPagingProducts.submit()\;\n';
jstr2 += 'return false\;\n';
jstr2 += '}\n';
jstr2 += '</script>\n';
jstr2 += '\n';
</script>

However the closing script tag in the created string actually close the javascript and I get errors such as:

Error: unterminated string literal
Line: 135, Column: 9 ( The </script> line before the end of the string.)
Source Code:
jstr2 += '

Is there any way I can prevent this issue..?

Many thanks for all your help.

Best Regards, Paul


edit I finally solved this problem by extracting the final </script> from the javascript string. I added a end tag where the script shows. Its messy, but it works. Many thanks for all your comments.

like image 828
neojakey Avatar asked Dec 10 '10 00:12

neojakey


3 Answers

The SCRIPT tag is content agnostic, so the parser just keeps running through the content until it finds a /SCRIPT 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 /SCRIPT begins. (There is no terminating quote mark to be found for the JS parser).

Escaping the slash with backslash

jstr2 += "<\/script>";

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).

like image 195
Michiel Kalkman Avatar answered Oct 11 '22 01:10

Michiel Kalkman


Write it as:

jstr2 += '<\/script>\n';
like image 6
Ryan Tenney Avatar answered Oct 11 '22 02:10

Ryan Tenney


You have to split the string:

jstr2 += '<' + '/script>\n';

It's also better to comment out everything inside the script:

<script type="text/javascript">
<!--//
    // your code here
//-->
</script>

Or in HTML:

<script type="text/javascript">
//<![CDATA[
    // your code here
//]]>
</script>

Or in XHTML:

  • same as HTML but #PCDATA instead of CDATA.
like image 4
vol7ron Avatar answered Oct 11 '22 00:10

vol7ron