Also check out the example here: Capture window/tab closing event - #2 by chriddyp. It hasn’t made it into the docs yet.