results.shift is not a function: firefox extension
I've written this code in Firefox JS extension
var results = gBrowser.contentDocument.getElementsByClassName("b-serp-item__title-link");
alert(results.length);
var countToDelete = results.length - 10;
alert(countToDelete);
if (countToDelete > 0)
{
for (var i = 0; i < countToDelete; i++);
{
alert("I prepare");
results.shift();
alert("I done");
}
}
alert("succ");
And I've got this output
results.length=12 countToDelete=2 (I prepare)
and... that's all There is a problem at results.shift(); I looked in Firefox Er开发者_C百科ror Console and I found this
"results.shift is not a function"
Why? Is shift a js function? When I try to run this code in firefox console I've got this error again
What's the matter?
My version of Firefox is 4. Tested url is http://yandex.ru/yandsearch?text=%D0%BE%D0%B1%D0%BE%D0%B9%D0%BD%D1%8B%D0%B9+%D0%BA%D0%BB%D0%B5%D0%B9+%D0%BA%D1%83%D0%BF%D0%B8%D1%82%D1%8C&lr=37
this will convert your nodelist
to a real Array
, which has a usable shift
method:
var results = Array.prototype.slice.call(
gBrowser
.contentDocument
.getElementsByClassName("b-serp-item__title-link")
);
I think it's clear that there is no such thing as shift()
in Gecko:
https://developer.mozilla.org/En/DOM/NodeList
The main question is what you want to achieve by it? By removing items form the NodeList
you are certainly not removing them from the DOM document. What is your quarrel with removeChild()
?
You need to convert the HTMLCollection to an array if you want to use shift() :
Most efficient way to convert an HTMLCollection to an Array
精彩评论