r/web_design • u/adambrenecki • Dec 16 '11
Posting code snippets properly on Reddit
Markdown formatting, which Reddit uses, supports including code snippets. All you have to do is indent by four spaces for blocks of code, or `surround in backticks` for code in the middle of a sentence. (If the code you want to surround includes a backtick, use ``two backticks (`)``).
<p class='example'>
code indented by four spaces displays like this
</p>
This makes things like line breaks in code appear like they're supposed to, puts it in a nice monospace font, and makes sure that things that would otherwise be interpreted as Markdown formatting aren't.
Can we please do this? It makes code snippets easier to read.
View all comments
1
u/Retrofire-Pink Oct 18 '21 edited Oct 18 '21
code blockI'm having some (habitual) trouble with a JavaScript load-order issue (use the image cause i can't figure out reddit code snippets)
HTML
<!DOCTYPE html><html lang="en-US"> <head> <title>rude game</title> <meta charset="utf-8", name="description", content="ghost bunny"> <link rel="stylesheet", href="styles.css"> <script src="script.js", defer></script> </head><body><canvas id="canvas", width="2000", height="2000"></canvas><img src="tiny.png", id="tinyTheAnt", weight="128", height="128"></body></html>JavaScript
const canvas = document.querySelector("#canvas");const canvCont = canvas.getContext("2d");const tinyTheAnt = document.querySelector("#tinyTheAnt");if (canvas.getContext) {tinyTheAntSprite = canvCont.drawImage(tinyTheAnt, -5, -75);console.log("great success");}Towards the end of my JavaScript, the console.log statement is passing correctly. So all my code appears to be functioning. I tested everything independently too, all checked out, EXCEPT the sprite drawing part. When I move the
<script src="script.js", defer></script>to the bottom of thebodyelement, however, it seems to work perfectly. This genuinely confuses the absolute hell out of me. Causedefer- from my noob understanding, essentially allows the entire DOM to load first (including all the necessary nodes being referenced), just as putting it at the bottom ofbodywould. Only, everything else works EXCEPT the sprite appearing, until I relocate the script reference with ostensibly the same effect as usingdefer. Whereas it works mostly OK, except every dozen refreshes the sprite mysteriously doesn't appear there either.