Use callbacks to accumulate output in htmljs.

This avoids lots of extra string creation; we accumulate all the output
in a single place from the beginning.