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

342
Visualizações
Are there any events faster than DOMContentLoaded that can be set as the argument of addEventListener? what is that?

Suppose that get a DOM as reference Node, then create and insert a DOM as new Node.

If I create and insert the new Node as soon as possible, is DOMContentLoaded the best event option in the above situation?

The reference Node is not image, it's HTMLnknowElement such as a HTMLDivElement, HTMLSpanElement.

Here is an example code flow.

window.addEventListener("DOMContentLoaded", (event) => {
 // Below is an example code as pseudo code.

 // Get a reference DOM.
 const referenceEl = document.querySelector(".foo .reference-dom");
 // Create a new DOM.
 const newEl = document.createElement(htmlTag);
 // Insert the new DOM before reference DOM.
 referenceEl.parentElement.insertBefore(newEl, referenceEl.nextElementSibling)
})

If I execute the code as soon as possible, is DOMContentLoaded the best event option in the above situation?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want the code to run as soon as possible, given what elements it depends on already existing in the DOM, the easiest way to do it would be to put your <script> tag just below the final element required for the script to run. For example, for the .reference-dom, you might do

<div class="reference-dom">
  ...
</div>
<script>
const referenceEl = document.querySelector(".foo .reference-dom");
// or
const referenceEl = document.currentScript.previousElementSibling;

// ... etc

If that's not an option, another avenue available to you would be to add a subtree MutationObserver to the document ahead of time (such as at the beginning of the <body>, and to watch the addedNodes to see if the node you're looking for appears - but that's expensive, overkill, and much more difficult to manage.

For very large pages (example), waiting for DOMContentLoaded could indeed take too long, in which case what you're doing would be a good idea - to add essential functionality to the app before that event fires, to ensure a good user experience. (Though, if what you're doing is just adding another tag to the HTML, a better approach would be to use a template engine or something server-side so that it serves the full HTML itself, rather than having to alter it client-side through JavaScript later)

about 4 years ago · Juan Pablo Isaza Relatório
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