Use
Window.οnunlοad = function () {...},
This function is called if the window is refreshed or browsing other pages.
If the window is turned off, the function will not be called.
Can't be clear about why this will be like this.
Use window.attachevent ('onunload', function () {...}); you can solve the problem.
REGARDS.
Lose network