DOM output stream class
Simulating an output stream in Javascript
Pass the id of a DOM element to the constructor to get started. Call the clear() method first, if necessary. Then call the write() method as many times as needed. Finally, always remember to call flush() to force your output to appear.
If it is not obvious, an important point of this class it to avoid inefficient repeated string concatenation. This is why it uses an array as a buffer, and why the write() method accepts multiple arguments--so you don't have to concatenate them yourself.
Pass the id of a DOM element to the constructor to get started. Call the clear() method first, if necessary. Then call the write() method as many times as needed. Finally, always remember to call flush() to force your output to appear.
// A little class for outputting HTML into a DOM element function DOMStream(id) { this.elt = document.getElementById(id); this.buffer = []; } DOMStream.prototype.clear = function() { this.elt.innerHTML = ""; }; DOMStream.prototype.write = function() { for(var i = 0; i < arguments.length; i++) this.buffer.push(arguments[i]); }; DOMStream.prototype.flush = function() { this.elt.innerHTML += this.buffer.join(""); this.buffer.length = 0; };
If it is not obvious, an important point of this class it to avoid inefficient repeated string concatenation. This is why it uses an array as a buffer, and why the write() method accepts multiple arguments--so you don't have to concatenate them yourself.
var out = new DOMStream("placeholder"); out.write('', section_title, ''); outputTable(out, data); // Function defined elsewhere out.flush();