Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!