I am trying to insert some labels script
but it gives me problems, the error it shows me is the following:
literal not terminated before end of script
What I can do?
<script>
function script() {
var content = "<script></script>"
}
</script>
I am trying to insert some labels script
but it gives me problems, the error it shows me is the following:
literal not terminated before end of script
What I can do?
<script>
function script() {
var content = "<script></script>"
}
</script>
You can do it using the method
createElement()
ofdocument
. Then you can add content to it and add it to the DOM.For example:
It is always
createElement()
the best option to add dynamic elements in the DOM. Notice that this code does what you want, without any complications:It can be solved by using a backslash (
\
) before the normal slash (/
)... Example: . This prevents the tag from closing.<\/script>
The exact problem is the tag
</script>
inside the string:The browser console shows:
I'm not sure it's a bug... rather I think it's an indeterminacy of the standard.
If the browser finds a tag
</script>
... Is it still inside the string, or did we forget to indicate the end of it?There are multiple solutions. From not using that exact sequence of characters (
'</script' + '>'
it is supported without problem) to usingcreateElement( )
.