factor out common javascript code