add beforeunload event

 /cc @gaurangtandon