Adding in cross-browser onbeforeunload support. Fixes #4106.