Javascript: ¿Por qué se detiene este JS? [cerrado]

This is for a Firefox addon, its a bit puzzling as after a certain point no code is being executed.

if (self.xmlDoc == null) return false;


    var domain_and_full_destination=processing_domain.split("  ");

    if(domain_and_full_destination[0]=="xxx.org")
    {
        //window.stop(); // Totally stop the page from loading.

        self.root_node = '';
        self.root_node = self.xmlDoc.getElementsByTagName('joe_biden_is_a_moron');
        var destinations_array= new Array();

        for (var cci = 0; cci <= self.root_node.length; cci++)
        {
            self.second_node = '';
            self.second_node = self.root_node[cci];
            destinations_array[cci]=self.second_node.getElementsByTagName('riaum')[0].firstChild.nodeValue;
        }


alert(domain_and_full_destination[0]+"\n");

Al if goes on, but I cut it short because I want to know why the alert is never getting called?

Putting my alert in the for loop gets called, but anything after the for loop never executes.

No errors in the Firefox error console either.

preguntado el 27 de agosto de 11 a las 19:08

Can you clarify which alert are you referring to? I see two of them above. -

Why don't you set a breakpoint in Firebug and find out for yourself? This is not a real question. -

Sorry, took out one alert. I dont know how to use Firebug... -

I guess you'll solve this one by learning how to use it. -

Aquí hay algunos consejos: stackoverflow.com/questions/6239118/… -

4 Respuestas

missing a close bracket for if statement...?

Ivo Stoykov

Respondido 27 ago 11, 23:08

No, i checked that first, I just posted the first few lines till it stopped working... no use posting the other stuff if its not reaching there... - Ryan

Mencionas eso for loop gets called but can you alert just before for loop ends to see if it iterates / exits the loop.

Placing alerts can help you debug if you are a beginner and do not know how to use debugging tools. However in the longer run you may want to use these tools to help you get the job done much faster.

Respondido 27 ago 11, 23:08

yep, it is popping the alert as long as i put it at the end of the for, but not outside - Ryan

For each iteration from 0 a self.root_node.length? As you mention that it throws an exception.. would mean at one point it should not pop an alert. - Amulya Khare

Are you sure it's not throwing? You're doing something wrong there.

for (var cci = 0; cci <= self.root_node.length; cci++)

debiera ser

for (var cci = 0; cci < self.root_node.length; cci++)

El hecho de que usaste <= there means that, on the last iteration of the loop, self.second_node is actually set to undefined, which would make self.second_node.getElementsByTagName('riaum') lanzar una excepción.

Respondido 27 ago 11, 23:08

Ok, made the change you suggested and now it is throwing: Error: self.second_node is undefined - Ryan

ok, using alerts I see that there were just 2 results but it was counting 0,1,2 which was causing the problem. Still dont know why exactly it did not show in the error consol in the beginning but your answer was the closest so am accepting it. Thakns everyone! - Ryan

try to devide this line into few variables

destinations_array[cci]=self.second_node.getElementsByTagName('riaum')[0].firstChild.nodeValue;

es decir,

var riaum = self.second_node.getElementsByTagName('riaum')[0] || null;
var child = riaum.firstChild || '';
var nodeVal = child.nodeValue || '?';
destinations_array[cci]=nodeVal;

you have to add checks what's in vars...

HTH

Ivo Stoykov

Respondido 28 ago 11, 00:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.