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

225
Vistas
JavaScript – ResizeObserver() NOT consistently executing a function when the page loads

This is the first time that I have attempted to use the ResizeObserver and I'm not sure what I am doing wrong.

The problem that I am seeing is that the ResizeObserver doesn’t CONSISTENTLY execute the function fnSetElementSizingVariables() when the page loads and as such variables that are necessary for the page are not available.

If I move the ResizeObserver outside of the window.addEventListener("DOMContentLoaded", (event), The ResizeObserver doesn't execute when the page is reloaded.

The ResizeObserver as I understand it is supposed to fire initially when the element that it is placed on is initially rendered on the page but the .js file is being referenced at the bottom of the html file so the element has already been rendered and if that's the case why is the fnSetElementSizingVariables() function more often than not executed when the page initially loads. As you can see I'm totally confused...

• Observation will fire when observation starts if Element is being rendered, and Element’s size is not 0,0.

Javascript ResizeObserver is triggered unexpected

The only way that I can assure that the variables are loaded is to create a new window.addEventListener("load") event and call the fnSetElementSizingVariables() function, but that then causes the variables, MORE OFTEN THAN NOT, to be created twice.

Any help with what I am doing wrong would be greatly appreciated. Thanks in advance - CES

const constContentContainer = document.getElementById("idContentContainer");

fnSetElementSizingVariables(){
    // Do Something
)

window.addEventListener("DOMContentLoaded", (event) => {

    window.addEventListener("resize", (event) => {
        fnSetElementSizingVariables();
    });

    const constResizeObject = new ResizeObserver(fnSetElementSizingVariables);
    constResizeObject.observe(constContentContainer);

})

    // If I move the ResizeObserver outside of the window.addEventListener("DOMContentLoaded", (event) => Above
    // The ResizeObserver doesn't exicute 

window.addEventListener("load", (event) => {

    // This was only added because variables we're not always being set by the ResizeObserver.
    fnSetElementSizingVariables();
});
#idContentContainer{

    overflow: auto;
    resize: both;

}
        <main id="idMainContent">

            <article>
                xxxx
            </article>

        </main>

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