Can any javaScript bod help me with this before my head explodes?

Soldato
Joined
17 Oct 2002
Posts
4,308
Location
Bristol
Basically I need to get the src and complete status of all images on a web page, I use selenium to pass and run the script in the browser, but that isn't really important only that the last thing in the script gets passed back to selenium (in this case my ',' delimited array of info).

This is what I have:

Code:
var my_window = this.browserbot.getCurrentWindow();
var iframe_count = (my_window.document.frames.length);
var image_complete = new Array();
for(var j=0; j<iframe_count; j++){
	var my_images = my_window.frames[j].document.images;;
	for(var i=0; i<my_images.length; i++){
		image_complete.push(my_images[i].src +\",\"+ my_images[i].complete);}}
image_complete;

But this will only get me all images in the base frames, I also need to get them from at least 1 level of nested frames.

At the moment I get all image info from:

my_window.frames[X].document.images

I need:

my_window.frames[X].frames[X].document.images

I just need another loop in there somewhere but my brain is hurting bad :(

Thank for any help
 
Last edited:
Something like this might work although the variables should prob be renamed and tidied up a bit - not tested though as im at work :p
Code:
var my_window = this.browserbot.getCurrentWindow();
var iframe_count = (my_window.document.frames.length);
var image_complete = new Array();
for(var j=0; j<iframe_count; j++)
{
    //get base frame images
    var my_images = my_window.frames[j].document.images;;
    for(var i=0; i<my_images.length; i++)
    {
        image_complete.push(my_images[i].src +\",\"+ my_images[i].complete);
    }
    //get sub frame images for this base frame
    var fcount = (my_window.document.frames[j].frames.length)
    for (var k=0; k<fcount; k++)
    {
        var my_images = my_window.frames[j].frames[k].document.images;;
        for(var i=0; i<my_images.length; i++)
        {
    	    image_complete.push(my_images[i].src +\",\"+ my_images[i].complete);
        }
    }
}
image_complete;
 
Last edited:
Just a slight mod to LadForces code. If you don't know how nested the frames are you could try this bit of recursion.

Code:
var my_window = this.browserbot.getCurrentWindow();
var iframe_count = (my_window.document.frames.length);
var image_complete = new Array();

function getImagesFromFromFrame(iFrame){

    var my_images = iFrame.document.images;
    for(var i=0; i<my_images.length; i++)
    {
        image_complete.push(my_images[i].src +\",\"+ my_images[i].complete);
    }

    var fcount = 0;
    fcount = iFrame.frames.length;

    if (fcount > 0) {
       for (var k=0; k<fcount; k++){
          getImagesFromFromFrame(iFrame.frames[k]);
       }
    }
}

function StartHere(){
    for (var i=0; i<iframe_count; i++)
    {
      getImagesFromFromFrame(my_window.document.frames[i]);
    }

    alert(image_complete.length);

}
 
Back
Top Bottom