Rearranged javascript events