Flash + Browser Interaction

Is it possible for flash to run code based on browser events? I want to run a logoff function if a user hits the logout button OR closes the browser window. Is this possible?

Yes it it possible try searching for running Action script function through Java script.