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

181
Visualizações
Why the browser has a beforeunload event even though I do not use it in my codes?

I have a function to help the user scroll up to the top of the page like this:

function toTheTop(){
        document.getElementByID('topUpBtn').addEventListener('click', ()=>{
            scrollTo( { top: document.querySelector('h1').getBoundingClientRect().top,
        behavior: 'smooth'})
    });
}

export {
    toTheTop
}

I am using Webpack for code organization. I called toTheTop function in an entry point as below:

import { toTheTop } from './js/TopUpButton.js';

if(document.readyState !== 'loading') {
    console.log('document is already ready');
    toTheTop();
} 
else{
    document.addEventListener('DOMContentLoaded', ()=>{
        console.log('document was not ready'),
        toTheTop();
    });
}

export {
    toTheTop
}

I have tested this function without Webpack and it worked. However, when using with Webpack, it didn't. Inspect the console for the button I only see:

DOMContentloaded
click

But with Webpack setup, I see a weird line named beforeunload in order like this:

beforunload
click

open this line I saw:

self.addEventListener("beforeunload", function () {
  status.isUnloading = true;
});

I did not implement this code at all. I looked for the use of beforeunload - this is only used if we want to prompt something for the user before leaving a page.

My questions are: Why do I have the beforeunload event-handler in my code? Does this relate to my Webpack setup? And how can I solve my problem?

I tried to figure it out for several days, but still can't handle it.

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

0

I have fixed it. Fortunately there is nothing wrong with my code. The function did not work because css elements conflicting with each other on different pages. It was hard to find but thank you guys. Posting a question helps me think it twice.

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