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

129
Visualizações
Grab DOM element after an external script has run

I have a Next.JS page that loads an external script. This external script modifies the DOM (Specifically places a div and an iframe on the page), it's a chat widget. The div that's added becomes the last child of the body, and I'm trying to grab it with document.body.lastChild. I'm running the logic in the onLoad property of Next's script component. Problem is, the timing seems to be off. It's grabbing the last child before the div is added, verified by logging.

Not sure why... I've tried running the logic in window onload listener, document onload listener, within useEffect, etc. Any ideas? I'm trying to grab the div so I can position it differently, for context. It renders in the bottom right of the screen by default.

<Script
     id="scriptid"
     src="scriptsource"
     onLoad={() => {
       functionFromScript();
       // my logic to grab the newly added div from the above function
       document.onload = () => {
       console.log('All loaded')
       const chatDiv = document.body.lastChild;
       console.log(chatDiv)
       chatDiv.style.position = 'relative';
     };
    }}>
</Script>
about 4 years ago · Santiago Gelvez
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