'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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source