Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to view results of document.write()?

Reply
Thread Tools

How to view results of document.write()?

 
 
Eli
Guest
Posts: n/a
 
      11-10-2004
I've got a script that I'm trying to debug which uses document.write()
to place HTML within a page. In both IE6 and Firefox when I view
source, I see only the script itself and not any HTML as it's being
written into the page.

Is there a way that I can view what is being placed into the page,
instead of, or in addition to the javascript code?

 
Reply With Quote
 
 
 
 
Hal Rosser
Guest
Posts: n/a
 
      11-10-2004

"Eli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I've got a script that I'm trying to debug which uses document.write()
> to place HTML within a page. In both IE6 and Firefox when I view
> source, I see only the script itself and not any HTML as it's being
> written into the page.
>
> Is there a way that I can view what is being placed into the page,
> instead of, or in addition to the javascript code?
>


call up the page in the browser.
that is what it writes.



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.788 / Virus Database: 533 - Release Date: 11/1/2004


 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      11-10-2004
Hal Rosser wrote:
> "Eli" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>I've got a script that I'm trying to debug which uses document.write()
>>to place HTML within a page. In both IE6 and Firefox when I view
>>source, I see only the script itself and not any HTML as it's being
>>written into the page.
>>
>>Is there a way that I can view what is being placed into the page,
>>instead of, or in addition to the javascript code?
>>

>
>
> call up the page in the browser.
> that is what it writes.


Do you ever actually test what you answer?

To the OP:
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
 
Reply With Quote
 
Eli
Guest
Posts: n/a
 
      11-10-2004
On Wed, 10 Nov 2004 01:17:38 -0500, Randy Webb
<(E-Mail Removed)> wrote:

>Hal Rosser wrote:
>> "Eli" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>
>>>I've got a script that I'm trying to debug which uses document.write()
>>>to place HTML within a page. In both IE6 and Firefox when I view
>>>source, I see only the script itself and not any HTML as it's being
>>>written into the page.
>>>
>>>Is there a way that I can view what is being placed into the page,
>>>instead of, or in addition to the javascript code?
>>>

>>
>>
>> call up the page in the browser.
>> that is what it writes.

>
>Do you ever actually test what you answer?
>
>To the OP:
>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.

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      11-10-2004
Eli wrote:
[...]
> I suppose it could be rewritten as such, however, if this is the best
> way.
>


Try loading in Firefox and use File -> Save as...


Cheers, Rob.
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      11-10-2004
Eli wrote:
> On Wed, 10 Nov 2004 01:17:38 -0500, Randy Webb
> <(E-Mail Removed)> wrote:
>
>
>>Hal Rosser wrote:
>>
>>>"Eli" <(E-Mail Removed)> wrote in message
>>>news:(E-Mail Removed) ...
>>>
>>>
>>>>I've got a script that I'm trying to debug which uses document.write()
>>>>to place HTML within a page. In both IE6 and Firefox when I view
>>>>source, I see only the script itself and not any HTML as it's being
>>>>written into the page.
>>>>
>>>>Is there a way that I can view what is being placed into the page,
>>>>instead of, or in addition to the javascript code?
>>>>
>>>
>>>
>>>call up the page in the browser.
>>>that is what it writes.

>>
>>Do you ever actually test what you answer?
>>
>>To the OP:
>>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.
>


It is always best to make a single string and then do one document.write
than doing multiple document.write's

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      11-10-2004
On Wed, 10 Nov 2004 00:05:26 -0700, Eli <(E-Mail Removed)> wrote:

> On Wed, 10 Nov 2004 01:17:38 -0500, Randy Webb
> <(E-Mail Removed)> wrote:


[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
Grant Wagner
Guest
Posts: n/a
 
      11-10-2004
Eli wrote:

> On Wed, 10 Nov 2004 01:17:38 -0500, Randy Webb
> <(E-Mail Removed)> wrote:
>
> >Hal Rosser wrote:
> >> "Eli" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >>
> >>>I've got a script that I'm trying to debug which uses document.write()
> >>>to place HTML within a page. In both IE6 and Firefox when I view
> >>>source, I see only the script itself and not any HTML as it's being
> >>>written into the page.
> >>>
> >>>Is there a way that I can view what is being placed into the page,
> >>>instead of, or in addition to the javascript code?
> >>>
> >>
> >>
> >> call up the page in the browser.
> >> that is what it writes.

> >
> >Do you ever actually test what you answer?
> >
> >To the OP:
> >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.


This doesn't work all that well, but it will show you the dynamic content (as
well as the script:

<button
onclick="window.open('javascriptpener.document.d ocumentElement.innerHTML.replace(/</g,
\'&\' + \'lt;\');');">View Dynamic Source</button>

Others can probably improve on this, but it's probably best to design your
script to output known HTML, rather than guessing and using some hack like
the above to try to make sure it's right.

--
Grant Wagner <(E-Mail Removed)>
comp.lang.javascript FAQ - http://jibbering.com/faq

 
Reply With Quote
 
Matt Kruse
Guest
Posts: n/a
 
      11-10-2004
Eli wrote:
> Is there a way that I can view what is being placed into the page,
> instead of, or in addition to the javascript code?


I use the code below in my IE context menu. You could easily turn it into a
bookmarklet if you wanted to. It shows me the source exactly as IE sees it,
which is what I usually need:

var srcwin =
window.open('about:blank','src','width=1200,height =900,top=0,left=0');
srcwin.document.open();
srcwin.document.writeln('<html><body><form><table width=100%
height=100%><tr><td><textarea name="src" id="src"
style="width:100%;height:100%;"
wrap="off"></textarea></td></tr></table></form></body></html>');
srcwin.document.close();
srcwin.document.forms[0].src.value =
doc.getElementsByTagName('html').item(0).outerHTML ;

--
Matt Kruse
http://www.JavascriptToolbox.com


 
Reply With Quote
 
RobB
Guest
Posts: n/a
 
      11-10-2004
Grant Wagner <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Eli wrote:
>
> > On Wed, 10 Nov 2004 01:17:38 -0500, Randy Webb
> > <(E-Mail Removed)> wrote:
> >
> > >Hal Rosser wrote:
> > >> "Eli" <(E-Mail Removed)> wrote in message
> > >> news:(E-Mail Removed)...
> > >>
> > >>>I've got a script that I'm trying to debug which uses document.write()
> > >>>to place HTML within a page. In both IE6 and Firefox when I view
> > >>>source, I see only the script itself and not any HTML as it's being
> > >>>written into the page.
> > >>>
> > >>>Is there a way that I can view what is being placed into the page,
> > >>>instead of, or in addition to the javascript code?
> > >>>
> > >>
> > >>
> > >> call up the page in the browser.
> > >> that is what it writes.
> > >
> > >Do you ever actually test what you answer?
> > >
> > >To the OP:
> > >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.

>
> This doesn't work all that well, but it will show you the dynamic content (as
> well as the script:
>
> <button
> onclick="window.open('javascriptpener.document.d ocumentElement.innerHTML.replace(/</g,
> \'&\' + \'lt;\');');">View Dynamic Source</button>
>
> Others can probably improve on this, but it's probably best to design your
> script to output known HTML, rather than guessing and using some hack like
> the above to try to make sure it's right.


You might try something like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>
<script type="text/javascript">
//<![CDATA[

var ptwin = null,
r = null,
gstr = '';

function plaintextwin()
{
if (null != r)
clearTimeout(r);
if (ptwin && !ptwin.closed)
ptwin.close();
var str = '';
for (var a = 0; a < arguments.length; ++a)
str += arguments[a].concat('\n');
gstr = gstr.concat(str.replace(/(<[^/])/g, '\n$1').concat('\n'));
ptwin = open('about:blank', 'ptwin',
'width=100,height=100,left=20,top=50,scrollbars,re sizable');
ptwin.document.open('text/plain');
ptwin.document.write(gstr);
ptwin.document.close();
ptwin.focus();
r = setTimeout('ptwin.resizeTo(' + (screen.width * .95) + ',' +
(screen.height * .66) + ')',100);
}

document.write = plaintextwin;

s = '<span class="foo">hah</span><h4>Hello, World !</h4><br />';
document.write(s);
document.write('<div class=vs6>&nbsp;</div><a
href="http://ftp.netscape.com/pub/netscape7/english/7.2/mac/Netscape-MachO.dmg.gz">Mac
OS X</a>');
document.write("I've got a script that I'm trying to debug which uses
document.write() to place HTML within a page.");
document.write('.vbmenu_hilite a:hover, .vbmenu_hilite a:active
{color: #FFFFFF; text-decoration: none;}');
document.write('alpha' , 'beta' , 'gamma');

//]]>
</script>
</head>
<body>
</body>
</html>

Works in IE, which allows a mimetype as an argument to
document.open(). Buggy in Firefox, no time to find out why. Should
allow you to at least see what's printing. I disagree about the 'one
big string' approach - document.write/ln take multiple arguments,
comma-separated as usual, an efficient way to avoid repeated
invocations.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How can I make this more efficient? (combining DataSet results with the results of a DB lookup.) Ken Fine ASP .Net 3 07-23-2008 08:11 AM
Prefix increment/decrement results in lvalue, but postfix one results in rvalue? lovecreatesbeauty C++ 8 09-12-2005 10:23 PM
Displaying results as "pages" of a JTable and sorting across all results ... Monique Y. Mudama Java 1 06-28-2005 01:01 AM
How to make a week view and day view calendar just like month view calendar in .NET ? Parthiv Joshi ASP .Net Web Controls 1 07-06-2004 03:15 PM



Advertisments