Deal with onbeforeunload properly