addEventListener not working with onbeforeunload

zconnelly13 picture zconnelly13 · Jun 6, 2012 · Viewed 30.9k times · Source
window.addEventListener("onbeforeunload",function() {return "are you sure?"});

^ This does not seem to work... at all... the page will simply close without displaying the confirmation box...

I realize that...

window.onbeforeunload = function() {return "are you sure?"}

Will work, but I want to add to the functionality (e.g. add many event listeners to the "onbeforeunload" function) not rewrite the function completely!

Answer

meder omuraliev picture meder omuraliev · Jun 6, 2012

Remove the on from onbeforeunload.

Also, be aware that addEventListener will not work in the older IE's and possibly other browsers. If you want consistent event binding use a library.