On Wed, 10 Nov 2004 01:17:38 -0500, Randy Webb
[snip]
The simplest way is to add a textarea to the page, instead of
document.write (or in addition to), set the textarea's value to your
variable.
Thank you. As it stands the code doesn't do a single document.write()
of a variable, but numerous writes in conditionals, loops, etc.
I suppose it could be rewritten as such, however, if this is the best
way.
If it's too much trouble to change your code, then change what the write
method does.
document.write = (function() {
/* Perform a one-time initialisation of the overriding
* function.
*
* This will hold the property name for the original
* write method so it can be called later.
*/
var old = '__write',
/* When this method is called, it is possible that the
* textarea used to hold the output has not been encountered
* yet, so it can not be addressed through the DOM. Until
* it is accessible, output is stored in the variable below.
*/
tmp = '',
/* This is the id of the textarea where output should
* be directed. Change it as you see fit.
*/
output = 'textarea-id',
/* This is text that should be appended to any text sent to
* the output textarea. You might just want to leave it as-is
* (a new line), but change this too if you want. Note that
* it won't affect what is actually written to the document.
*/
suffix = '\n',
/* Similar to the above, but prepended to the text. */
prefix = '';
/* Search for an identifier that is not currently used
* where the original function can be stored.
*/
while('undefined' != typeof document[old]) {old += '__';}
/* Once found, add a reference to the original method. */
document[old] = document.write;
/* Now that the initialisation is complete, the function
* below overrides the original method.
*/
return function(str) {
/* Attempt to get a reference to the debugging
* textarea element.
*/
var o = document.getElementById(output);
/* Store the text in case the call above fails. */
tmp += prefix + str + suffix;
/* Write the text using the original method. */
document[old](str);
/* If the textarea could be found, we can begin preparations
* to use it in every call after this point. As we won't need
* to store the text any more, we can prevent that code from
* being called by substituting yet another function.
*/
if(output) {
/* Send the stored text to the textarea. If you want to use
* the textarea for other things, you might want to change
* this line from an assignment (=) to a concatenation (+=).
*/
output.value = tmp;
/* As we may have built up a lot of text,
* let's free some memory.
*/
tmp = null;
/* From now on, this whole process can be streamlined.
* As we now have access to the textarea element, text
* can be added to it directly.
*/
return function(str) {
output.value += prefix + str + suffix;
document[old](str);
}
}
};
})();
You'll probably have to change the string, output, and possibly the prefix
and suffix strings. Copy this code into a SCRIPT element in the HEAD
section of the document, and call document.write as normal. Depending
where your document.write calls are in respect to the debugging TEXTAREA
element, you may need to add a call at the end of your page - even just
document.write('') - to flush the buffer.
If you use the writeln method, too, you can pretty much copy the above,
changing "write" to "writeln" where needed. The code is actually quite
compact - it's just the comments that make it so imposing.
Good luck,
Mike