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

181
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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