warning.onclick = confirm('Do you want to leave');
This calls confirm()
immediately, and assigns the result (either true or false) to onclick
(which is meaningless).
Instead, you need to create an anonymous function that calls confirm()
and returns the result.