[Snowman] JS error when writing a script inside a variable

Twine Version: 2.3.9
Story Format: Snowman 2.0.3

In the Twine editor, create a new story, select Snowman, and write this in the Story Javascript:

window.sampleFunction = function() {
	temp = "<script></script>"	
}

Is there any reason for this to break? Well, it does. Firefox says this:

In Story JavaScript Eval(): SyntaxError: "" literal not terminated before end of script

This is a minimal example. In real use I put a lot of things between the script tags, and then the function prints that to the screen. This kind of function has worked for me many times, and I don’t understand why it’s failing now.

Thanks!

1 Like

The Story JavaScript gets put in a <script> tag, and in HTML, the contents of scripts are treated as plain text, and the characters</ must not appear except as part of the </script> tag which ends the script section. So you need to break up any </ somehow. If it’s in a JavaScript string, the easiest thing is to put a backslash before the slash, like:

window.sampleFunction = function() {
	temp = "<script><\/script>"	
}

If you’re not writing a </script> tag you might be able to get away without breaking the </ pair: browsers are pretty lenient usually. But you’ll probably get some sort of warning message somewhere.

Edit: I’m not sure why this sort of thing worked for you before, sorry. That’s very weird: AFAIK it should always have broken…

3 Likes

Oh shit. You are totally right, of course. I just hadn’t noticed that my old code from previous games had that backslash in <\/script>.

Thanks!