Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda