new .js file for widget

Moved API code to a javascript file.