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

213
Vistas
JavaScript: How can I prompts a custom message box when they close broswer?

Problem:

I am new in JavaScript. And I want to give a message like "you have down xxx works today!",
when my users close broswer. I've search google/stackoverflow and get some solutions in Prompt User before browser close?

BUT, when I try code as followd, it just shows the broswer's default message, not mine. How can I solve it?

Code:

function test(evt) {
    var message = 'Did you remember to download your form?';
    if (typeof evt == 'undefined') {
        evt = window.event;
    }
    if (evt) {
        evt.returnValue = message;
    }
    return message;
}

Are there some useful APIs or solutions for me?

Edit (2021.9.22) I try the tips posted By @NiceBooks like

window.addEventListener('beforeunload', function (e) {
    window.alert("ready to exit");
    window.confirm("just a test");
    e.returnValue = '';
});

IE and FireFox prompt their default message. If e.returnValue is not set, there is no prompt box when I click exit button. beforeunload says:

'In some browsers, calls to window.alert(), window.confirm(), and window.prompt() may be ignored during this event. See the HTML specification for more details. '

Maybe it's the reason why I failed. But is there really no solution?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can try with the window.confirm(msg) function.

https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm

Use it in conjunction with the beforeunload event on the window object:

beforeunload in 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