add event listener on beforeunload