[Greasemonkey] Basic JavaScript questions
mmccarthy at reifysoft.com
mmccarthy at reifysoft.com
Wed Jun 29 16:40:00 EDT 2005
> As mentioned in an earlier message, getElementsByTagName
> is a dynamic array, not a static snapshot.
Whoops, I didn't know that the *returned value* is dynamic. So there's no
difference between the following, either in functionality or efficiency?
(Never mind that they won't do what the OP wants.)
1)
var elements = document.getElementsByTagName("h3");
for (var i = 0; i < elements.length; i++)
{
elements[i].parentNode.removeChild(elements[i]);
}
2)
for (var i = 0; i < document.getElementsByTagName("h3").length; i++)
{
document.getElementsByTagName("h3")
[i].parentNode.removeChild(document.getElementsByTagName("h3")[i]);
}
Matt McCarthy
When I am king, you will be first against the wall
With your opinion, which is of no consequence at all.
-Radiohead, "Paranoid Android"
-----Original Message-----
From: greasemonkey-bounces at mozdev.org
[mailto:greasemonkey-bounces at mozdev.org] On Behalf Of Mark Pilgrim
Sent: Wednesday, June 29, 2005 1:51 PM
To: greasemonkey at mozdev.org
Subject: Re: [Greasemonkey] Basic JavaScript questions
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
_______________________________________________
Greasemonkey mailing list
Greasemonkey at mozdev.org
http://mozdev.org/mailman/listinfo/greasemonkey
More information about the Greasemonkey
mailing list