Forum Moderators: open

Message Too Old, No Replies

Using for loop to load images, but doesn't seem to load more than one

Is this a javascript caching problem?

         

otem

10:16 pm on Feb 13, 2007 (gmt 0)

10+ Year Member



Hello,

I have the following script to run on document load.

for(i=0;i<document.getElementsByTagName("iframe").length;i++) if(document.getElementsByTagName("iframe")[i].src.indexOf("domain.tld")>-1){
bug=new Image();
bug.src="/get.php?type=info;
}

I have the get.php page set to not cache using several different PHP headers.

However, on pages where I know there are two iframes with a src that contains domain.tld, I am only seeing one request from the javascript.

I'm wondering if how it is worded, if it just assumes that it is the same file, and just uses the first copy.

Any idea if I'm on target, or what else it might be?

Does anyone know a solution? Thanks.

otem

12:55 am on Feb 14, 2007 (gmt 0)

10+ Year Member



Dark thoughts happen when you have spent the last couple hours on a simple script that should have taken five minutes.

I have tried so many different options and am no closer to figuring solving this script and its bugging me so much.

otem

1:03 am on Feb 14, 2007 (gmt 0)

10+ Year Member



I did come across a breakthrough:

I found that if I place an alert at the end of the bug, then it would properly loop through.

for(i=0;i<document.getElementsByTagName("iframe").length;i++) if(document.getElementsByTagName("iframe")[i].src.indexOf("domain.tld")>-1){
bug=new Image();
bug.src="/get.php?type=info;
alert(i);
}

Unfortunatly, I really can't have alerts load up on all my pages. I tried other things, like var x=i;, but that didn't seem to work.

daveVk

5:04 am on Feb 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try something like this to eliminate possible cache problems

var date = new Date();
var bug;

for(var i=0;i<document.getElementsByTagName("iframe").length;i++) if(document.getElementsByTagName("iframe")[i].src.indexOf("domain.tld")>-1){
bug=new Image();
bug.src="/get.php?type=info&junk=" + date.getTime() + i;
}

Having created bug you dont seem to do anything with it, I assume there is some extra code not shown.

[edited by: daveVk at 5:04 am (utc) on Feb. 14, 2007]

otem

5:05 pm on Feb 15, 2007 (gmt 0)

10+ Year Member



Thanks for your help.

Turns out the script was working perfectly. Many hours spent trying to fix something that was working.

The problem was my database that was pulling the information. It was setup so the time the index, so it wasn't allowing two entries with the same time in. That's why the alert worked, because it staggered the time.

Its moments like these that I was I was doing something else.

Silvery

12:44 am on Feb 17, 2007 (gmt 0)

10+ Year Member



I recently had something similar happen due to javascript apparently not refreshing the screen until the looping function completed executing. I was trying to make table cell backgrounds to change colors in sequence, and each loop would set a new color. Prob was that the function would begin, no apparent cell color changes would happen, then when the function completed it would just display the table cells in their last color state. To the eye, all the intermediary steps' colors never changed.

Essentially, I solved this by using setTimeout to reinitiate the function rapidly for each looping, where "grid" is the name of my table in the HTML document. I would call the function "cycle" with the number of times I wanted the script to loop:

var x=document.getElementById('grid').cells;

function changecolor(whichcell) {

x[whichcell].className='black';

}

function cycle(num) {

newnum = num - 1;

changecolor(newnum);

if (nunum >= 1) {

timer = setTimeout('cycle(nunum)', 5)

}
}