Use beforeunload event

I'm not 100% certain about this improvement but wherever I have seen this pattern it is "beforeunload event + synchronous Ajax call".