Event with and without addEventlistener are not the same?

2

Because this does not work:

window.addEventListener('beforeunload', function(){
  return 'wait a minute'
})

And this works:

window.onbeforeunload = function(){
  return 'wait a minute'
}

Are not they the same thing?

    
asked by anonymous 16.12.2017 / 19:50

1 answer

2

The two do not work the same way. The addEventListener with event beforeunload requires event.returnValue :

window.addEventListener('beforeunload', function(event){

   var mensagem = "wait a minute";
   event.returnValue = mensagem;
   return mensagem;

});

More information this MDN document in English .

    
16.12.2017 / 20:58