Change ordering of function declaration and event listener assignment

When a function is assigned to a event listener, but it is not yet declared
it'll break sometimes in firefox and firefox mobile.

closed #1110