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 = '<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>';\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></script></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('<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>');\n\n var innerHtml = "<li><a href=\\"#view1\\" id='miniprofile-view1' onclick=\\"viewMiniProfile('"+id+"',this)\\">Mini-Profile</a></li>" +\n "<li><a href=\\"#view2\\">Tokens</a></li>" +\n "<li><a href=\\"#view3\\">Notes</a></li><li><a href=\\"#view4\\">PM Logs</a></li>";\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><script>\n// ...stuff here...\n$('#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>');\n// ...stuff here...\n</script>\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></script></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('<div id="horizontalUserPopup"><ul id="tabs1" class="rtabs"></ul><div class="panel-container"><div id="view1"><script>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('...<script>...<\\/script>...');\n</code></pre>\n\n<p>The <code>\\</code> prevents the browser's parser from seeing the sequence <code></script></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" } } } }