<<nobr>> macro prevents javascript from executing

tweego, version 2.1.1+81d1d71 (2020-02-25T07:09:26Z) [darwin/amd64]
“name”:“SugarCube”,“version”:“2.36.1”

The following passage doesn’t print “Starting 2” on load. There is no error; it fails silently. Removing the <<nobr>> enables the final console log to print “Starting 2”.

:: Start
[[P Test]]

:: P Test
<h2>Test</h2>
<<nobr>>
<script>
    console.log("Starting");
    console.log("Starting1");
    // Example JSON data
    var jsonData = {};
    console.log("Starting 2");
</script>
<</nobr>>

Nothing in the SugarCube2 documentation indicates that JavaScript execution is affected. Any idea why this is happening, and how to solve it?

1 Like

Because only after spending hours did you realise that the <<nobr>> tag will wipe out ALL new lines, including the ones after lines of comments, making all script AFTER the comment ALSO commented!

1 Like

Have you tried using <<script>>?

The <<nobr>> macro doesnt prevent JavaScript, just removes the <br> HTML Markup. (Edit from the future, it also affects the lines in scripts, TIL)
The issue here is the <script>. Twine doesn’t use that Markup in its projects. You should use the <<script>> macro instead (docu).

1 Like

It’s definitely the comments. I’ve just tried the comment-infused script with <<script>> and it didn’t work, whereas removing the comment sort-of worked (it brought about another error related to my actual valid JSON, so looks like <<script>> wouldn’t have worked either).

If you press F12 and go to the Elements/Inspector tab, you’ll see the compiled HTML, including your <script> contents. Maybe that will shed some light on what the <<nobr>> is doing to the code.

<<nobr>> doesn’t prevent JavaScript, but it does strip out all the newlines. So if you have a <<script>> tag inside a <<nobr>>, and you use a // comment (comment until end of line) then the nobr replaces all the newlines with spaces, so then the comment runs until the end of the script tag, instead of until the end of the original line.

@Atawf yeah, that sucks. I spent quite a while trying to debug that myself at one point…

5 Likes

Oh man, I had the exact same issue. Fixed it on my own but if this thread had existed back then it would’ve saved me a few hours of desperately searching “Twine Sugarcube JS not work”. Anyway, for anyone looking at this in the future: when you have <<script>> or <script> nested inside <<nobr>>, use multi-line comments /* like this */ instead of single-line comments or you too might spend a few hours running around with your Javascript on fire.

5 Likes