Event with and without addeventlistener are not equal?

Asked

Viewed 282 times

2

Because this doesn’t work:

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

And this works:

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

They’re not the same?

1 answer

2


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

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

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

});

More information in this MDN documentin English.

  • Show!! That’s right, thanks!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.