var snaphtml = '<script src="http://seadragon.com/embed/lxe.js?width=auto&height=400px"></script>';
give a syntax error? Escaping the final
The problem may be that the web browser sees the "
</script>" sequence and decides that's the end of the script block.
Another way to fix the problem aside from using an escape sequence like you did is to break it apart into 2 strings that are concatenated:
"<" + "/script>"
The behavior you're seeing isn't a bug n the part of the browser.
Browsers don't "look inside" a script block, they just pass the content to the script engine. The "
</script>" sequence is how they know they've come to the end of the block, and since the browser doesn't interpret the contents of the block, it has no way to know that it's in the context of a literal string in the script code.
You're actually running into an html-escaping issue: the browser interprets
</script> in your string as the close-tag for the
var snaphtml = '<script src="http://seadragon.com/embed/lxe.js?width=auto&height=400px">
To fix it, as you've found, you just need to change
</script> to anything else, like
The HTML parser will interpret the end of tag token (ETAGO delimiter
</) in your string, as the end of the current script tag, giving you the unterminated string
There are several workarounds, including the use of
CDATA blocks, but the simplest way is to escape that character, or make a string concatenation:
var snaphtml = '<script src="...">\x3C/script>'; var snaphtml = '<script src="..."><' + '/script>';
And of course, you can also create your
script element programmatically and append it to the head:
var newScript = document.createElement("script"); newScript.src = "..."; //...
See Everything you always wanted to know about
</, aka. the end-tag open (ETAGO) delimiter for a detailed explanation. TL;DR there’s no need for crazy hacks like string concatenation or char literal escapes — just escape it as such:
var snaphtml = '<\/script>';
Also, note that this is only necessary for inline scripts.
©2020 All rights reserved.