# Stack Snippets messing with JS?

I've noticed that my Red vs. Blue Sandbox controller snippet here has stopped working (at least in Chrome).

This is incredibly strange because the JSFiddle that is exactly identical is working fine. More importantly, if you go to the edit mode for the meta question, the snippet works fine when you click Run code snippet in the render preview, or if you click edit the above snippetRun.

The error that the browser gives is

Uncaught SyntaxError: Unexpected token ILLEGAL


this makes perfect sense when you look at the JavaScript causing it:

var battle, redTeam, blueTeam, interval

1385d3a1-d76e-4049-926f-d58c6ff78ba5('#showCircles')
loadEntries()
})


I have no idea where the 1385d3a1-d76e-4049-926f-d58c6ff78ba5 came from. The code should look something like this:

var battle, redTeam, blueTeam, interval

$(document).ready(function() { ctx =$('#canvas')[0].getContext('2d')
moveCounter = $('#moveCounter') showNames =$('#showNames')
showCircles = $('#showCircles') loadEntries() })  The edit page has the correct code but the real question doesn't. I'm guessing it has something to do with jQuery or MathJax because of the $ involved.

Interestingly, the official question snippet works fine. The code there is minified though so this may not be that surprising.

Any clue what's going on?

(Not sure but a similar error may be happening here.)

• In case it helps, I can reproduce this problem (even the odd hash-looking thing is identical) Apr 8 '15 at 20:48
• Definitely a MathJax-related problem (I've seen a similar strange effect on CR). Looking into it...
– Doorknob Mod
Apr 8 '15 at 20:50
• This is definitely what broke my snippet.
– orlp
Apr 9 '15 at 0:10

## 3 Answers

This is a MathJax-related issue. Here are some very short snippets that reproduce the bug, which appears to occur whenever multiple dollar signs are placed in a single code block:

$foo$bar
$baz  and $
foo
bar
baz
$ Note that the original source of this answer does not include any extra indentation, yet the rendered HTML does show it. According to a comment by @m0sa, Stack Exchange developer: I looked into it, turned out to be a deep rabbit hole requiring at major refactoring of MarkdownSharp (we'd need to make the pipeline extensible, so we could inject stuff before DoCodeBlocks in RunBlockGamut - currently all the processing happens before we ever hit MarkdownSharp, but we'd need to leverage it's block processing) So, it's unlikely that this will be fixed any time soon (ugh). Unfortunately, the answer is just "use jQuery or some other alias in your code instead of $" until this is fixed.

• Now I'm not so happy we got MathJax. I think bug-free stack snippets are much more important for PPCG. :( Apr 8 '15 at 21:24
• I was actually going to report this as a bug for code block rendering... Apr 8 '15 at 21:31
• And I don't get how it can render correctly in the edit preview but not in the actual question. You'd think it'd be the exact same procedure for rendering both of them. Apr 8 '15 at 21:32
• @Calvin'sHobbies It's not. Client-side for preview, server-side for actual post.
– Doorknob Mod
Apr 8 '15 at 21:33
• @Calvin'sHobbies It doesn't. Try editing this answer. Apr 8 '15 at 21:33
• I see. @MartinBüttner The JS in the RvB Sandbox was rendered correctly in the edit preview (there was no hash thing). Apr 8 '15 at 21:37
• Are the developers aware that dollar signs break the search as well?
– grc
Apr 9 '15 at 1:24
• @MartinBüttner, Doorknob, there is a similar issue on Music stackexchange and there is a very simple workaround. See my answer and check if it works for you. Sep 29 '15 at 18:32

On the meta only, I have edited the MathJax delimiter to use \$$ and \$$. Let me know if this allows your snippets to work better!

Testing alternative MathJax invocation syntax: $$\sqrt{x^2+y^2}$$

Display mode still works:

$\sqrt{x^2+y^2}$

Another test from Martin:

abcdef
$$ab de$$
$$ab de$$
$ab de$

• I tried using $ again instead of jQuery in my meta snippet but the same exact issue (strangely?) still occurred. Apr 10 '15 at 19:01 • On your test from Martin, was it supposed to be formatted as code? Apr 10 '15 at 19:11 • @ASCIIThenANSI Yes. Apr 10 '15 at 20:36 Not sure if this has been fixed, it's not showing up in the edit box $foo
$bar$baz


(no, it's not fixed, $bar has additional indentation than what I saw in the edit!) There is a similar issue on Music Stack exchange, where unwanted guitar chords pop up. The workaround is to use <pre> and </pre> tags around content you don't want mangled. This also means you don't need to include 4 leading spaces in front of your code. (Yes, works!) $foo
$bar$baz


(Please click the edit button to see exactly what I have done.)

• Having to escape &, <, and other HTML characters isn't fun... nor is the lack of syntax highlighting. (Not to mention the potential to break several of the Stack Snippets that we commonly use.)
– Doorknob Mod
Sep 29 '15 at 19:18