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

Javascript new window - how to wait for page to load? 1

Status
Not open for further replies.

tcstom

Programmer
Aug 22, 2003
235
GB
Hi, I have Javascript that is meant to open a new window which contains a form, and then manipulate a couple of form elements. However, the page in the new window takes a while to load. Is it possible to tell my function in the originating page to wait for the page in the new window to load before running the rest of the script? (I assume this is the problem, but I've posted the Javascript function in case I've done something wrong in there).

Code:
function OpenForm(url)
{
  newwindow = window.open(url);
  newdocument = newwindow.document;

  newdocument.forms(0).myformitem.value = 'test';
}

Tom
emo_big_smile.gif
 
I do not know the answer to your question but have another thought on the approach.

Have your first page launch the window and use the onload event in the window to call a different function to do the processing you need.
Breaking it up into two separate functions makes it a lot easier.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Ah, I should have mentioned, the page that loads in the new window is written by generic server-side code. I really want to avoid inserting any application-specific code on that side, hence my need to manipulate it from the originating window. Hope that makes sense. Thanks for the input but I really need an answer to my original question...

Tom
emo_big_smile.gif
 
I've found a work around to this problem. The code...

Code:
newwindow = window.open(url);
newdocument = newwindow.document;
setTimeout("AutoFillForm(newdocument)",8000);

...waits 8 seconds (enough to let the page load) then calls a separate AutoFillForm() function, sending it a reference to the new form. Does the job!

Tom
emo_big_smile.gif
 
First make the popup window reference global. Then check the readiness of the element, if not try some time later.
[tt]
var newwindow;
function OpenForm(url)
{
newwindow = window.open(url);
newdocument = newwindow.document;
//replaced
//newdocument.forms(0).myformitem.value = 'test';
setEntry('test');
}
function setEntry(s) {
if (newwindow && !newwindow.closed && newwindow.document && newwindow.document.forms[0] && newwindow.forms[0].myformitem) {
newwindow.document.forms[0].myformitem.value=s;
} else if (newwindow && !newwindow.closed) {
setTimeout("setEntry('" + s + "');", 200);
}
}
[/tt]
(On ie specifically, document.readystate may be used, but it is not cross-browser. The above should be cross-browser.)
 
This is probably an IE only solution, but worth checking out: check out the readyState property of the document. It should return one of: [tt]uninitialized, loading, loaded, interactive, complete[/tt]


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
tcstom, the delay will help but if for any reason the server is slow responding or the clients PC processes the page slowly the timer could expire before the page is actually loaded.

tsuji's suggestion is a good one. It opens the window and then tests that it can access the elements.

I would set up a function that sets a 1 second delay then calls the test function to see if the form is ready and if it evaluates false it would set another delay for 1 second and test again in a loop until it evaluates true then execute the rest of your code.
This way the delay occurs in one second increments so if the page loads faster then it executes the rest of the code without any additional delay but if it runs long the code will not execute until it becomes available.

You could put a counter into the function to check for a certain number of executions and then quit just in case something goes wrong like the popup window being blocked and therefor never becoming available.



Paranoid? ME?? WHO WANTS TO KNOW????
 
Thanks for all the input. I've actually worked out my own solution which seems to take care of everything. The following code goes into a loop, each time checking if the target form exists yet. When it does exist it breaks out of the loop and calls the AutoFill function. Just in case of any server-related probs it breaks out automatically after 20000 loops.

Code:
  function OpenForm(url)
  {
    newwindow = window.open(url);
    newdocument = newwindow.document;

    var i = 0;
    var formLoaded = false;
    while (formLoaded == false)
    {
      formLoaded = IsFormLoaded(newdocument);
      i++
      if (i > 20000)  // break out if hanging
      {
        break;
      }
    }
    if (formLoaded == true)
    {
      AutoFillForm(newdocument)
    }
  }

  function IsFormLoaded(doc)
  {
    if (doc.forms(0) == undefined)
    {
      return false;
    }
    else
    {
      return true;
   }
}

Tom
emo_big_smile.gif
 
Thanks clFlaVA! That's real kind because I feel really a bit down for a while confused if I am not being understood or I am not up to the task of spelling out clearly what I see and what offer.

I also take this opportunity to correct my typos committed in real-time on this line.

[tt]if (newwindow && !newwindow.closed && newwindow.document && newwindow.document.forms[0] && newwindow.[red]document[/red].forms[0].myformitem) {[/tt]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top