I am working with onbeforeunload and What I was able to figure out is:
- onbeforeunload handler is blocking the browser from destroying the current page
- if you don't return anything, the popup does not appear.
So your code will be working as long as the event handler runs.
This means that timer functions are not usable. They just add to the execution queue, so anything they would do is being queued after the end of currently running handler, which is after the last point in time you were guaranteed your code is still running.
So there is only one way to stop the browser from unloading before the animation finishes:
- put a blocking loop that wastes some time in the
beforeunload
handler
- start CSS3 animation by setting an appropriate class on the element before the loop
- make the loop end when the animation finishes (make the loop check the actual height of an element or something)
Oh, and yes, this is a nastiest hack of all, but I was able to find a way to stop the browser from unloading the page, right?
I would appreciate comments with ideas on what to put in the loop.
I am taking some options into account:
- wasting CPU on come math on large numbers
- accessing localstorage (synchronous call, IO operration)
- accessing DOM (this solution already has to)
Any ideas?