Przed rozładowaniem

addEventListener('beforeunload', event => { });
Travis C