Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
In Javascript, can I use a While Loop to wait for a function in an iframe htm to load?

Background

I'm a relative js newbie. As such, I'm not well versed in the more advanced concepts like asynchronous javascript etc. But I'm trying to learn. Anyway, as part of my learning, I'm experimenting if it's possible to wait for a function inside another file to become available with a simple while loop?

If not, why not?

About my Code

  • I have a parseQuery() function inside <script> tags inside the body of an .htm file.
  • The .htm file is loaded inside an iframe.
  • In my main script, I want to wait for and then call parseQuery once it's available.

I'm keying off of one of the suggestions in this post to check if the function is undefined: Test for undefined function in Javascript

Here's my basic while loop approach:

// ... earlier code gets a handle on the iframe element
// ... and I pass the query params to it with setAttribute

// we now need to wait for the parseQuery() function to become available
    let i = 0;
    while (typeof parseQuery === undefined){
        console.log('It is still not defined. Waiting ' + i);
        i++;
    }
    console.log('After while loop, typeof ParseQuery is: ' + typeof parseQuery);

My console log after the loop always shows: "After while loop, typeof parseQuery is undefined" There's no indication it ever gets into the while loop at all. I find this strange considering it's undefined. How is that possible? I thought it go into the loop and then essentially wait until the parseQuery function became available and then it'd exit the loop once it did.

Is my condition incorrect, or is something else I'm not understanding going on?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!