Fix error in onReleased.

Only do something when the holder is not null.
Move all the logic in a javascript file.