How to execute script on quit?

How can I execute a function when the player ends the game, in javascript?