Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with For Loops and iFrames Please

Status
Not open for further replies.

wellmoon

Technical User
Dec 6, 2006
28
GB
Hi,

I want to load a series of pages into an iFrame and then do something with the contents. The following code works fine with just one page loaded into the iFrame:
Code:
function loadDocs() {
    frames("docframe").location.href = docs[0];
    setTimeout("doNextFunction();", 1000);
}
Like I say, this works fine and calls the next function after ample time for the iFrame doc to load. However, docs is an array and as soon as I put the above code into a for loop so that all items in the array can be loaded (one after the other with doNextFunction() acting on all of them), it stops working. This is what I have tried:
Code:
function loadDocs() {
  for (x = 0; x < docs.length; x++) {
    frames("docframe").location.href = docs[x];
    setTimeout("doNextFunction();", 1000);
  }
}
Anyone see what I'm doing wrong? Any pointers would be *hugely* appreciated
 
first off, i don't see how the notation [tt]frames("...")[/tt] works, because that acts as if you're trying to call a function, not refer to a collection. maybe it works in forgiving, non-standards-compliant IE, but that'd be all.

try this:

Code:
function loadDocs() {
  for (x = 0; x < docs.length; x++) {
    top.frames["docframe"].location.href = docs[x];
    setTimeout("doNextFunction();", 1000);
  }
}

although, i don't see what you're trying to accomplish with this either, because you'll be setting the location so quickly that you won't see them change.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks for the cross-browser tip there,

visitors don't need to see the contents of the iframe change, in fact I will probably set the iframe height and width to 0. But the script needs to access each doc that gets loaded into the iframe before the next doc is loaded.

What about a do loop instead of a for loop?
 
Yeah, I thought of that but the pages that get loaded in the iframe are individual pages in their own right and need to be able to be opened in a browser on their own sometimes so an onload event in them would cause errors...

There must be a way...
 
how would an onload event within the iframe cause errors? you can't just write off ideas because of brash assumptions.

play around with something like this:

Code:
<iframe onload="doSomething();"></iframe>



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
one more thing,

do you use the variable "x" in any function that calls your loadDocs() function???

Known is handfull, Unknown is worldfull
 
I dont use x in any functions that call this function, but I do use it elsewhere in other unrelated functions...

I will experiment with the onload function, I see what you mean now about placement, when you mentioned it yesterdsay I thought you meant putting it each of the pages loaded into the iframe, not in the iframe element in the master page....
 
wellmoon, here's an example:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html>
<head>
<title>Untitled</title>

<script type="text/javascript"><!--

var docs = new Array('[URL unfurl="true"]http://www.yahoo.com/',[/URL] '[URL unfurl="true"]http://www.google.com/',[/URL] '[URL unfurl="true"]http://www.reddit.com/');[/URL]
var x = 0;

function doThing(i) {
    if ( x == docs.length ) x = 0;
    i.src = docs[x];
    x++;
}

//--></script>

</head>

<body>

<iframe src="[URL unfurl="true"]http://www.yahoo.com/"[/URL] onload="doThing(this)" style="height: 100px; width: 300px;"></iframe>

</body>
</html>



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
cLFlaVA thanks a lot, nothing beats an example! I got it going now, much appreciated :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top