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

179
Views
Cómo evitar que beforeunload se dispare cuando se selecciona guardar

Actualmente tengo una función que establece "isDirty = true" cuando se realiza un cambio en la página. Funciona como se esperaba . Tengo una función que solo se activa cuando se navega fuera de la página actual después de no elegir el botón Guardar.

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

Lo anterior funciona, sin embargo, cuando hago clic en cancelar en la alerta para permanecer en la página y luego guardo la página, el cuadro de diálogo aparece nuevamente.

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

0

Puede especificar la opción once en la llamada addEventListener :

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

EventTarget.addEventListener() - API web | 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!