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

176
Views
How to prevent beforeunload from firing when save is selected

I currently have a function that sets "isDirty = true" when a change is made on the page. Works as expected I have a function that only fires when navigating away from the current page after not choosing the save button.

function pageChange() {
            if (isDirty == true) {
                window.addEventListener('beforeunload', function (e) {
                    // Cancel the event
                    e.preventDefault(); 
                    e.returnValue = '';
                    isDirty = false;
                });
            } else {
                return
            }
        }

The above works, however, when I click cancel on the alert to stay on the page and then go save the page, the dialog box shows up again.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could specify the once option in the addEventListener call:

function pageChange() {
    if (isDirty == true) {
        window.addEventListener('beforeunload', function (e) {
            ...
        }, { once: true });
    }
}

EventTarget.addEventListener() - Web APIs | MDN

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!