0

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 Error 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

takayoshi
  • 2,789
  • 5
  • 36
  • 56
  • No, it's much like an array: it has a length, and you can get elements of it using `collection[0]`, `collection[1]`, etc. but it's not actually an array. – Tyler Apr 26 '11 at 06:52

3 Answers3

1

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()?

vbence
  • 20,084
  • 9
  • 69
  • 118
1

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")
              );
KooiInc
  • 119,216
  • 31
  • 141
  • 177
0

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

Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236