Using the beforeunload event trigger instead of custom code