views:

102

answers:

1

Why does the following JavaScript, when run in Firefox 3.6.3, delete all FRAMESET elements in a document, but the similar script that instead uses an XPath expression to select the FRAMESET elements, does not? Is document.evaluate() simply unable to match FRAMESET elements? Is there an error in the XPath expression? Is there some other error?

Select all FRAMESET elements using method document.getElementsByTagName() (succeeds):

var framesets = document.getElementsByTagName('frameset');
for (var i = 0; i < framesets.length; i++) {
  framesets[i].parentNode.removeChild(framesets[i]);
}

Select all FRAMESET elements using an XPath expression (fails):

var framesets = document.evaluate("//frameset", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = 0; i < framesets.length; i++) {
  framesets[i].parentNode.removeChild(framesets[i]);
}
+1  A: 

It appears that the XPathResult is iterator-ish. It does not have a length property.

https://developer.mozilla.org/en/DOM/document.evaluate

framesets.length will be undefined. In short, Here Be Dragons.

wombleton
Yes, you are right. Was just about to give a similar answer, but you beat me to it!
Derek Mahar