Smarter onbeforeunload handling.

Fixes #17.