check for existence of unload event before binding

Some JavaScript runtime environments (eg Google Chrome Packaged Apps) do not expose the 'unload' or 'beforeunload' events. Attempting to bind to these throws an exception. These changes check for the existence of those events before trying to bind to them.