Here a couple of example:
// daily way ...
var div = document.createElement("div");
document.body.appendChild(div);
// easy way
var div = document.body.appendChild(document.createElement("div"));
// boring way
var div = document.createElement("div");
div.appendChild(document.createTextNode("test"));
document.body.appendChild(div);
// funny way
var div = document.body.appendChild(
document.createElement("div")
).appendChild(
document.createTextNode("test")
).parentNode
;
... on and on, there are several methods used on daily basis that do not consider returned values. So, why don't we like to use them?
var el = document.createElement("div"),
div = document.createElement("div");
alert([
el === document.documentElement.appendChild(el), // el in the DOM
el === el.parentNode.removeChild(el), // el removed
div === document.documentElement.appendChild(div), // div in the DOM
div === div.parentNode.replaceChild(el, div), // div removed, el in the DOM
div === el.parentNode.insertBefore(div, el) // both in the DOM, div returned
].join("\n"));
As summary, try to find a leak possibility with this:
with((document.body || document.documentElement)
.appendChild(
document.createElement("div")
)
.appendChild(
document.createTextNode("not")
)) parentNode
.insertBefore(
document.createTextNode("Why "),
parentNode.firstChild
)
.parentNode
.appendChild(
document.createTextNode(" ?")
)
;
Not even a single variable declared, three elements inserted in a specific node of the DOM and in a different order :D
No comments:
Post a Comment