Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda