Adding beforeunload/unload event handling.