JavaScript: deferred document.write implementation