I believe this could be done using the beforeunload event. It might not catch absolutely everything, but most of the regular reloads should work with it. You can look at this more detailed answer for code examples