Move Javascript code into a function.

This allows the user to call it when they wish, with the parameters they want.