Search code examples
javascripteventsgoogle-chromeaddeventlisteneronbeforeunload

addEventListener not working with onbeforeunload


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!


Solution

  • 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.