Unregister events when window is unloading

    Fixes #546

    Keep a list of events that are registered and on beforeunload
    call removeEventListener for each of them.

    There is no need to worry about if the user calls remove since
    calling remove with an event and func that doesn't exist does
    nothing, keeps the code simpler.  The same goes for worrying
    about grouping / keeping track of duplicates since the extra
    calls to remove does nothing.