user2740154
user2740154

Reputation: 21

Firefox "TypeError: all[i] is undefined"

I have a browser based service manual for my Jeep. Works great in IE but not Firefox. Basically there is a TOC that you can expand and drill down into sub topics, e.g.

But Firefox will throw the following error when clicking on the [+] beside the main topics:

TypeError: all[i] is undefined
itcToc.js line 10
if( "UL" == all[ i ].tagName )     

Here is the code in itcToc.js:

var eCurrentUL, eCurrentLI, eUL, tempIndex, syncIndex = null;
var eTagName, eSrc, eImgIndex, eSynchedNode = null;
var tempImgIndex = '0';

function getNextULNS(eSrc)
{
    var all = document.getElementsByTagName("*");
    for(var i = tempIndex; i < all.length; i++)
    {
        if( "UL" == all[ i ].tagName )
        {
        eImgIndex = all[tempIndex - 2];
            return all[ i ];
        }   
        else if( "LI" == all[ i ].tagName )
        {
            break;
        }
    }
    return false;
}

I'm a mechanic so tried my best to troubleshoot. If I replace i with an integer on line 10 it will get past this and error on the next if statement at line 15, which has similar syntax.

Is this a variable scope issue? Seems like Firefox does not like var i in the if statement despite be defined in the for statement.

Upvotes: 2

Views: 789

Answers (3)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382394

You must set tempIndex to a valid number instead of undefined if you want all[ i ] to work for the first iteration.

For example

var tempIndex = 0;

With the friends of the JS room, we tested [0,1][undefined] on IE versions 7 to 10 and it always gave us undefined. So either you're testing on an older browser or there's something missing in the question.

Upvotes: 0

Andy
Andy

Reputation: 63569

var i = tempIndex: tempIndex hasn't been defined anywhere. Unless you mean tempImgIndex in which case you got the variable name wrong. And if it's meant to be tempImgIndex then it needs to be var tempImgIndex = 0 not var tempImgIndex = '0'.

Upvotes: 0

Tibos
Tibos

Reputation: 27843

tempIndex is not initialized at the begining of the for loop, causing i to be undefined as well.

Upvotes: 0

Related Questions