Add unload and beforeunload support