[Greasemonkey] Basic JavaScript questions

Mark Pilgrim pilgrim at gmail.com
Wed Jun 29 17:50:58 EDT 2005


On 6/29/05, chris feldmann <cfeldmann at gmail.com> wrote:
>  Something about using document.getElementsByTagName() makes it unuseful for
> this. XPath on the other hand provides an array that can be cleanly iterated
> through.

As mentioned in an earlier message, getElementsByTagName is a dynamic
array, not a static snapshot.  Which means that the original code is
not only wrong, it's doing a lot of extra work to be wrong
(recalculating the length every time through the loop).

document.evaluate('//h3', document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null) will return something
that you can iterate through and remove things from without modifying
the original.  (That's what the "SNAPSHOT_TYPE" part means.)  This is
Firefox-specific and will not work in Turnabout/IE or Opera.

Or, as someone else pointed out, you could add a CSS rule "h3 {
display: none ! important }" and not worry about the fact that they're
still in the DOM.

-- 
Cheers,
-Mark


More information about the Greasemonkey mailing list