Creating document in the memory with createHTMLDocument()
Use following code: var doc = document.implementation.createHTMLDocument("New Document"); Suppored in all browsers. (IE9+)
Use following code: var doc = document.implementation.createHTMLDocument("New Document"); Suppored in all browsers. (IE9+)
Considering following example: <ul id="menu"> <li>Node 1</li> <li>Node 2</li> </ul> and following code: var el = document.getElementById("menu"); el.innerHTML returns: <li>Node 1</li> <li>Node 2</li> el.outerHTML returns: <ul id="menu"> <li>Node 1</li> <li>Node 2</li> </ul> Suppored in all browsers.
insertAdjacentHTML() parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. It does not reparse the element it is being used on and thus it does not corrupt the existing elements inside the element. This, and avoiding the extra step of serialization make it… Read More »
Consider example: var element = document.getElementById("something"); if (document.body.contains(element)) { //do something } Suppored in all browsers.
Consider following code: <ul> <!– Comment –> <li>Node 1</li> <li>Node 2</li> </ul> .children.length returns 2 but .childNodes.length returns 5 everything is because there are text nodes before, after and between LI elements. Remember that IE 6-8 threats comments as nodes, so children.length in these browsers returns 3 instead of 2. Suppored in all browsers. Similar… Read More »
The formula is very simple, and returns almost identic results (tested on OSX). size = (element_height / element_scroll_height) * element_height and similar for horizontal one: size = (element_width / element_scroll_width) * element_width In JS: var size = (el.offsetHeight / el.scrollHeight) * el.offsetHeight; In JS + JQ: var size = ($el.height() / el.scrollHeight) * $el.height();
To archive better performance you should do: var arr = [1, 2, 3]; arr.length = 0;
var arr = [1, 2]; Object.prototype.toString.call(arr) === "[object Array]"
Closures are stored in memory, so be careful with them.
Works for all types: !!0 //false !!1 //true etc Works for booleans: +true //1 +false //0 -(n+1) ~1 //2 ~-9 //8