How to get Javascipt output to file?

J

joe

I often would like see the HTML output produces by my Javascipt code for
debugging. Is this possible?
 
E

Evertjan.

joe wrote on 02 apr 2008 in comp.lang.javascript:
I often would like see the HTML output produces by my Javascipt code for
debugging. Is this possible?

Try a browser.

===============

Oh perhaps you wnt to see the reconstructed HTML from the DOM?

[Javascript generally does not produce HTML code,
but can and often does change the DOM.]

Send the obj.innerHTML to a new window.
 
L

Lasse Reichstein Nielsen

joe said:
I often would like see the HTML output produces by my Javascipt code for
debugging. Is this possible?

Try this as a bookmarklet:

javascript:(document.documentElement||document.body).innerHTML.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/\n/g,"<br>");

(I.e., either paste it into the address line, or, for reuse, create it as
a bookmark)

/L
 
S

SAM

Lasse Reichstein Nielsen a écrit :
Try this as a bookmarklet:

javascript:(document.documentElement||document.body).innerHTML.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/\n/g,"<br>");

Arrrggghh !

where are gone the so lovely indentations of my html code ?

:)
 
U

Une Bévue

joe said:
I often would like see the HTML output produces by my Javascipt code for
debugging. Is this possible?

Usually i do have a <pre id="log"></pre> for testing purpose in my page

and instead of doing alert (the timing is broken) i do :

var myText="Starting point at "+new Date().getTime();

myText+="another blahblah";

....

by the end :

document.getElementById("log").appendChild(document.createTextNode(txt))
;

that's all...
 
L

Lars Rune Nøstdal

SAM said:
Lars Rune Nøstdal a écrit :

Tremendous ! I had it in my HD and not yet tried.
Does that reveal code gotten via XHR ? (a kind of virtual code)

yes

by the way .. check out firebug if you haven't already, it's great
 
L

Lasse Reichstein Nielsen

SAM said:
Lasse Reichstein Nielsen a écrit :

Arrrggghh !

where are gone the so lovely indentations of my html code ?

Depends. They might have gone away when the page was parsed (innerHTML
uses the DOM, which might or might not retain whitespace).

But you can try:

javascript:"<pre>"+(document.documentElement||document.body).innerHTML.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/\n/g,"<br>").replace(/\x20/g,'\xa0')+"</pre>";

which should be more readable.
/L
 
S

SAM

Lars Rune Nøstdal a écrit :
yes

by the way .. check out firebug if you haven't already, it's great

Firebug unlovely I can't print its analyzes
Dom inspector
Web developer
Color Zilla, Tidy, Js View, Style Sheet Choser, ...

There are so much good extensions.
 
S

SAM

Lasse Reichstein Nielsen a écrit :
Depends. They might have gone away when the page was parsed (innerHTML
uses the DOM, which might or might not retain whitespace).

You say : "they have gone" (away) but can we say "they are gone" ?
But you can try:

javascript:"<pre>"+(document.documentElement||document.body).innerHTML.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/\n/g,"<br>").replace(/\x20/g,'\xa0')+"</pre>";

Perfect !

except : where is my doctype ?
which should be more readable.

not more missing than color (syntactic coloring) :)


Question :

I've tried to use : replace(/</g,"&lt;")
on a textarea value but that didn't work, something somewhere seeming to
think that is a tag and by the way not to be translated or parsed.
As soon as '<' is followed by a character '<' and following are ignored.

<textarea onkeyup="var txt = this.value.replace(/</g,"&lt;");
document.getElementById('here').innerHTML = txt;"></textarea>

What could be done to fix that ?
I've found something that seems very complex.

onkeyup="var txt = this.value;
txt = txt.replace(/</g,'&lt;');
txt = txt.split(/[\r\n]/);
var D = document.getElementById('blackboard');
while(D.hasChildNodes()) D.removeChild(D.firstChild);
for(var i=0; i<txt.length; i++) {
var t = document.createTextNode(txt);
var n = document.createElement('BR');
D.appendChild(t);
D.appendChild(n);
}"
 
J

joe

Evertjan. said:
joe wrote on 02 apr 2008 in comp.lang.javascript:
I often would like see the HTML output produces by my Javascipt code for
debugging. Is this possible?

Try a browser.

===============

Oh perhaps you wnt to see the reconstructed HTML from the DOM?

[Javascript generally does not produce HTML code,
but can and often does change the DOM.]

Send the obj.innerHTML to a new window.

What I mean is when I have:
document.write(' <tr>');

I wnat to see text:
<tr>


For example when I "View source" from IE I get this:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Calendar</title>
<link href="test2.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script type="text/javascript" src="testing.js">
</script>
</body>
</html>


I would have wanted to see also the HTML testing.js produced.
 
J

joe

Lasse Reichstein Nielsen said:
Try this as a bookmarklet:

javascript:(document.documentElement||document.body).innerHTML.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/\n/g,"<br>");

(I.e., either paste it into the address line, or, for reuse, create it as
a bookmark)

/L

Frankly I did not understand it and thought is was a wrong answer but wow - it
works - just what I needed. Thanks.
 
B

beegee

Usually i do have a <pre id="log"></pre> for testing purpose in my page

and instead of doing alert (the timing is broken) i do :

var myText="Starting point at "+new Date().getTime();

myText+="another blahblah";

...

by the end :

document.getElementById("log").appendChild(document.createTextNode(txt))

I kind of like this except for the vagueness around "starting point"
and "end". I assume you mean within functions after onload, not in
global javascript.? Thanks.

Bob
 
U

Une Bévue

beegee said:
I kind of like this except for the vagueness around "starting point"
and "end". I assume you mean within functions after onload, not in
global javascript.? Thanks.

yes, after onload...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top