Move JavaScript functions into an external file

As per #176.