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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Waiting for iframe to load

Status
Not open for further replies.

DarVar

Programmer
Mar 14, 2006
10
IE
I have the following three functions

function getFieldValue(fieldName) {
var myField = document.getElementById(fieldName);
callServlet(myField);
return myField.value; //doesn't wait
}

function callServlet(myField){
var url = '/EvaluationLibrary?settingField='+myField;
var myIframe = document.getElementById("myHiddenFrame");
myIframe.src="}

The function above basically loads the iframe with the servlets response html. The html returned by the servlet uses the onload attribute. Onload calls the third function:

<body onload=\"parent.setFieldValue('" + result + "','" + fieldName + "');\"/>"

function setFieldValue(result,fieldName){
var fieldToSet = document.getElementById(fieldName);
fieldToSet.value = result;
}

The problem is that in function getFieldValue returns an empty string because it doesn't wait for the servlet call to the iframe and then for the iframe to finish loading and then call the setFieldValue function to populate the field.

Any ideas on making getFieldValue wait for setFieldValue?
 
You could call the getFieldValue in the onLoad method of your hiddern frame.

Cheers,
Dian
 
That is not an option for me as the getFieldValue needs to return to another function which basically gets all the values of the fields in the page and posts them off to another servlet.

The approach I'd like is to find a way to get the callServlet
function to wait for the iframe to load.
 
I have a function as follows:

function setFrame(){
loadFrame();
alert("frame loaded");
}

function loadFrame(){
var iframe = document.getElementById("myIFrame");
iframe.src="}

The problem is that the alert message appears before the iframe has loaded the google page.

Is there something I can do in loadFrame() that after setting the iframe src I make the function wait till the iframe loads.
i.e. iframe.src=" //when iframe has loaded continue

I tried usign onreadystate but no luck
 
For cross ie-moz solution, try this.
[tt]
function setFrame(){
loadFrame();
alert("frame loaded");
}
function loadFrame(){
var iframe = document.getElementById("myIFrame");
if (document.all) {
iframe.onreadystatechange=function() { if (iframe.readyState=="complete" || iframe.readyState=="loaded") handle(); };
} else {
iframe.onload=handle;
}
iframe.src="}
function handle() {
alert("frame loaded");
}
[/tt]
 
I didn't look over tsuji's response except to see that it is different than what I would have first recommended, so for the sake of options, here's what I usually do in this situation. The ASSUMPTION here, however, is that you have control over the source code of the page IN the IFRAME as well.

In the parent page:
Code:
var waiting = false;

function one()
{
 loadIframe();
 waiting = true;
 waitForIframe();
}

function waitForIframe()
{
 if(waiting)
  setTimeout("waitForIframe()", 100);
 else
  function two();
} 

function two()
{
 //set whatever you need to, to the value you need to
}

Then, in the IFRAME page, set the BODY tag's ONLOAD event to parent.waiting = false;

This is a little different to how your function returns a value, but I'm guessing there's a different way you could code that to do what you want. I'd have to see the code where you call getFieldValue(...) to make any specific suggestions in that regard, however.

'hope this helps.

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Just trigger me to look at what I have posted. There is a left over on cut-and-paste in the first function I followed the op. But I meant the alert should be commented out and let the function handle to act as the alert.
[tt]
function setFrame(){
loadFrame();
[red]//[/red]alert("frame loaded");
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top