“Unterminated template literal” syntax error when literal contains script tag [duplicate]

If you insert </script> inside a script tag, no matter if it’s a string in quotes, apostrophes, or even a template literal, it will always close the script tag. You have to escape it, for example like that:

var str=`
<script>
<\/script>
`
var pre = document.createElement('pre')
pre.textContent = str
document.body.appendChild(pre)

However, if you use external script <script src="https://stackoverflow.com/questions/36607932/url"></script>, it should work fine without escaping.

Leave a Comment