[Greasemonkey] innerHTML: is this good?

esquifit esquifit at googlemail.com
Sat Jul 8 16:18:16 EDT 2006


Whether  innerHTML will become standard, I cannot say.  Regarding
performance of innerHTML vs DOM methods, a quasi-standard reference (and a
must-read, doubtless) is quirksmode [1].


Since FF1.5 (also in GM) there is also support  for e4x (ECMAScript for XML)
[2], which is in itself a standard (at least there is a formal
specification).  A starting point for further references is [3].  You are
then allowed to write things like:

const MY_ICON = ...;
var url = ...;
var xmlParser = new DOMParser;
var tree = <testing xmlns=" http://www.w3.org/1999/xhtml" />;
tree.test = <div><a href={url}><img src={MY_ICON}  /></a></div>;
var dom_tree = xmlParser.parseFromString(tree.toXMLString(),'text/xml');

dom_tree is now an XMLDocument object, and dom_tree.firstChild is an
HTMLUnknownElement object that can be inserted via appendChild into any
other node of your HTML document.

The code above is adapted from Johan Sundström's "Mark my links" script [4].

[1] http://www.quirksmode.org/dom/innerhtml.html
[2] http://www.ecma-international.org/publications/standards/Ecma-357.htm
[3] http://en.wikipedia.org/wiki/E4X
[4] http://userscripts.org/scripts/show/3382


2006/7/8, carlos campderrós < gilipollas.desconcertante at gmail.com>:
>
> Hi everybody.
>
> In my first userscript I used innerHTML to add content to a web. Some
> time later I read somewhere that innerHTML was evil, non-standard and
> not DOM-ish, so better to not use it. Then I replaced the innerHTML
> with lots of document.createElement(), element.setAttribute() and
> element.appendChild (), that is really tiring and somehow ofuscates the
> code.
>
> Yesterday I took a look at dive into greasemonkey book and saw an
> example with innerHTML. My life is a mess now ;-)
>
> Will, sometime in the future, innerHTML become standard? Will firefox
> and greasemonkey support it forever? Is safe to use it?
>
> And talking about performance. Which way is better: innerHTML or lots
> of createElement and so?
>
> Long question short: What are the pros and cons of using either
> innerHTML or document.createElement... ?
>
> --
> Si no puedes deslumbrar con tu inteligencia,
> desconcierta con tus gilipolleces
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
>


More information about the Greasemonkey mailing list