tag not working within a string variable", "text": "<p>Please look my code.</p>\n\n<pre class="prettyprint"><code>var id = 1;\nvar htmlText = '&lt;div id="horizontalUserPopup"&gt;&lt;ul id="tabs1" class="rtabs"&gt;&lt;/ul&gt;&lt;div class="panel-container"&gt;&lt;div id="view1"&gt;&lt;script&gt;viewMiniProfile("'+id+'",this);&lt;/script&gt;&lt;/div&gt;&lt;div id="view2"&gt;&lt;/div&gt;&lt;div id="view3" style="display:none;"&gt;Blah Blah Blah&lt;/div&gt;&lt;div id="view4" style="display:none;"&gt;444444&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;';\n$('#contents').html(htmlText);\n</code></pre>\n\n<p>Above code i am getting following error - </p>\n\n<p><img src="https://i.stack.imgur.com/BWosB.png" alt="enter image description here"></p>\n\n<p>If i remove <code>&lt;/script&gt;</code> its working fine. Please check and let me know.</p>\n\n<p><strong>EDIT:</strong></p>\n\n<p>Complete Code - </p>\n\n<pre class="prettyprint"><code>function modelInfo(id, username) {\n var pageUrl = $('#pageurl').val();\n $('#model-popup-box1 h3').addClass('newsfeed');\n var content_area = $('#model-popup-content1');\n content_area.html('Please wait...');\n $('#model-popup-box-title1').html('About ' + username);\n $('#model-popup-body1').show();\n\n content_area.html('&lt;div id="horizontalUserPopup"&gt;&lt;ul id="tabs1" class="rtabs"&gt;&lt;/ul&gt;&lt;div class="panel-container"&gt;&lt;div id="view1"&gt;&lt;script&gt;viewMiniProfile("'+id+'",this);&lt;/script&gt;&lt;/div&gt;&lt;div id="view2"&gt;&lt;/div&gt;&lt;div id="view3" style="display:none;"&gt;Blah Blah Blah&lt;/div&gt;&lt;div id="view4" style="display:none;"&gt;444444&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;');\n\n var innerHtml = "&lt;li&gt;&lt;a href=\\"#view1\\" id='miniprofile-view1' onclick=\\"viewMiniProfile('"+id+"',this)\\"&gt;Mini-Profile&lt;/a&gt;&lt;/li&gt;" +\n "&lt;li&gt;&lt;a href=\\"#view2\\"&gt;Tokens&lt;/a&gt;&lt;/li&gt;" +\n "&lt;li&gt;&lt;a href=\\"#view3\\"&gt;Notes&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\\"#view4\\"&gt;PM Logs&lt;/a&gt;&lt;/li&gt;";\n\n var ul = document.getElementById("tabs1");\n ul.innerHTML = innerHtml;\n\n\n $('#horizontalUserPopup').responsiveTabs({\n rotate: false,\n startCollapsed: 'accordion',\n collapsible: 'accordion',\n setHash: true,\n disabled: [4, 5]\n });\n\n }\n</code></pre>", "answerCount": 1, "upvoteCount": 758, "dateCreated": "2015-05-14 06:53:36", "dateModified": "2022-09-20 14:13:34", "author": { "type": "Person", "name": "Developer" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>I'm going to assume your quoted code is in a <code>script</code> tag, like this:</p>\n\n<pre class="prettyprint"><code>&lt;script&gt;\n// ...stuff here...\n$('#contents').html('&lt;div id="horizontalUserPopup"&gt;&lt;ul id="tabs1" class="rtabs"&gt;&lt;/ul&gt;&lt;div class="panel-container"&gt;&lt;div id="view1"&gt;&lt;script&gt;viewMiniProfile("'+id+'",this);&lt;/script&gt;&lt;/div&gt;&lt;div id="view2"&gt;&lt;/div&gt;&lt;div id="view3" style="display:none;"&gt;Blah Blah Blah&lt;/div&gt;&lt;div id="view4" style="display:none;"&gt;444444&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;');\n// ...stuff here...\n&lt;/script&gt;\n</code></pre>\n\n<p>If so, the problem is that the outer <code>script</code> tag is terminated in the middle of your string, because the HTML parser in the browser doesn't interpret JavaScript code, it just scans through it looking for <code>&lt;/script&gt;</code> to figure out where the tag ends. So the code that gets handed to the JavaScript parser is</p>\n\n<pre class="prettyprint"><code>$('#contents').html('&lt;div id="horizontalUserPopup"&gt;&lt;ul id="tabs1" class="rtabs"&gt;&lt;/ul&gt;&lt;div class="panel-container"&gt;&lt;div id="view1"&gt;&lt;script&gt;viewMiniProfile("'+id+'",this);\n</code></pre>\n\n<p>...which does indeed have an unterminated string literal.</p>\n\n<p>You can fix that by:</p>\n\n<ol>\n<li><p>Putting your JavaScript code in a <code>.js</code> file and linking to it, or</p></li>\n<li>\n<p>Putting a backslash before the <code>/</code> in the ending script tag in your string:</p>\n\n<pre class="prettyprint"><code>$('#contents').html('...&lt;script&gt;...&lt;\\/script&gt;...');\n</code></pre>\n\n<p>The <code>\\</code> prevents the browser's parser from seeing the sequence <code>&lt;/script&gt;</code>, and to so it doesn't think the script ended there. In a JavaScript string, however, <code>\\/</code> is just <code>/</code>.</p>\n</li>\n</ol>", "upvoteCount": 153, "url": "https://exchangetuts.com/syntaxerror-unterminated-string-literal-scriptscript-tag-not-working-within-a-string-variable-1640664903755754#answer-1653802928929448", "dateCreated": "2022-09-16 14:13:34", "dateModified": "2022-09-20 14:13:34", "author": { "type": "Person", "name": "T.J. Crowder" } } } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: unterminated string literal <script>...</script> tag not working within a string variable

Please look my code.

var id = 1;
var htmlText = '<div id="horizontalUserPopup"><ul id="tabs1" class="rtabs"></ul><div class="panel-container"><div id="view1"><script>viewMiniProfile("'+id+'",this);</script></div><div id="view2"></div><div id="view3" style="display:none;">Blah Blah Blah</div><div id="view4" style="display:none;">444444</div></div></div>';
$('#contents').html(htmlText);

Above code i am getting following error -

enter image description here

If i remove </script> its working fine. Please check and let me know.

EDIT:

Complete Code -

function modelInfo(id, username) {
                var pageUrl = $('#pageurl').val();
                $('#model-popup-box1 h3').addClass('newsfeed');
                var content_area = $('#model-popup-content1');
                content_area.html('Please wait...');
                $('#model-popup-box-title1').html('About ' + username);
                $('#model-popup-body1').show();

                content_area.html('<div id="horizontalUserPopup"><ul id="tabs1" class="rtabs"></ul><div class="panel-container"><div id="view1"><script>viewMiniProfile("'+id+'",this);</script></div><div id="view2"></div><div id="view3" style="display:none;">Blah Blah Blah</div><div id="view4" style="display:none;">444444</div></div></div>');

                var innerHtml = "<li><a href=\"#view1\" id='miniprofile-view1' onclick=\"viewMiniProfile('"+id+"',this)\">Mini-Profile</a></li>" +
                        "<li><a href=\"#view2\">Tokens</a></li>" +
                        "<li><a href=\"#view3\">Notes</a></li><li><a href=\"#view4\">PM Logs</a></li>";

                var ul = document.getElementById("tabs1");
                ul.innerHTML = innerHtml;


                $('#horizontalUserPopup').responsiveTabs({
                    rotate: false,
                    startCollapsed: 'accordion',
                    collapsible: 'accordion',
                    setHash: true,
                    disabled: [4, 5]
                });

            }
like image 758
Developer Avatar asked May 14 '15 06:05

Developer


1 Answers

I'm going to assume your quoted code is in a script tag, like this:

<script>
// ...stuff here...
$('#contents').html('<div id="horizontalUserPopup"><ul id="tabs1" class="rtabs"></ul><div class="panel-container"><div id="view1"><script>viewMiniProfile("'+id+'",this);</script></div><div id="view2"></div><div id="view3" style="display:none;">Blah Blah Blah</div><div id="view4" style="display:none;">444444</div></div></div>');
// ...stuff here...
</script>

If so, the problem is that the outer script tag is terminated in the middle of your string, because the HTML parser in the browser doesn't interpret JavaScript code, it just scans through it looking for </script> to figure out where the tag ends. So the code that gets handed to the JavaScript parser is

$('#contents').html('<div id="horizontalUserPopup"><ul id="tabs1" class="rtabs"></ul><div class="panel-container"><div id="view1"><script>viewMiniProfile("'+id+'",this);

...which does indeed have an unterminated string literal.

You can fix that by:

  1. Putting your JavaScript code in a .js file and linking to it, or

  2. Putting a backslash before the / in the ending script tag in your string:

    $('#contents').html('...<script>...<\/script>...');
    

    The \ prevents the browser's parser from seeing the sequence </script>, and to so it doesn't think the script ended there. In a JavaScript string, however, \/ is just /.

like image 153
T.J. Crowder Avatar answered Sep 20 '22 14:09

T.J. Crowder