Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

282
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda