Skip to content

Instantly share code, notes, and snippets.

@eligrey
Created September 17, 2011 18:54
Show Gist options
  • Save eligrey/1224224 to your computer and use it in GitHub Desktop.
Save eligrey/1224224 to your computer and use it in GitHub Desktop.
JS demo DOM tips

JS demo DOM tips

A few tips for shaving some bytes off your JS demos. I will probably add more to this list in the future.

Escaping HTML (saves 60-93B, depending on host document type)

26B:

new Option(html).innerHTML

86B minified (in an HTML document):

x = document.createElement("_");
x.appendChild(document.createTextNode(html));
x.innerHTML

119B minified:

x = document.createElementNS("http://www.w3.org/1999/xhtml", "_");
x.appendChild(document.createTextNode(html));
x.innerHTML

Creating text nodes (saves 3B)

26B:

new Option(text).lastChild

29B:

document.createTextNode(text)

The HTML namespace (saves 6B)

For when your demo is SVG and you need to create HTML.

24B:

new Image().namespaceURI

30B:

"http://www.w3.org/1999/xhtml"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment