JavaScript document.write(): How to view the HTML?

JavaScript document.write(): How to view the HTML?



How to view the html generated by Javascript document.write() command?

When you use the JavaScript document.write() code to output something to the browser, it does not give you a friendly way to debug your JavaScript. If you view source on the page, browser shows you the JavaScript code again; it does not show you what your document.write() results in.

For example, if your JavaScript code is something like the uggly stuff below:

v_temp='<div '+(v_nS4?"name":"id")+'=v_container style="position:'+v_position+'; top:'+v_top+'px; left:'+v_left+'px; width:'+v_width+'px; height:'+v_height+'px; background:'+v_bgColor+'; layer-background'+ (v_bgColor.indexOf("url(")==0?"-image":"-color")+': '+v_bgColor+';clip:rect(0,'+v_width+','+v_height+',0); overflow:hidden">';for(v_i=0; v_i'+(v_content[v_i][0]!=''?' < a href="'+v_content[v_i][0]+'" target="'+v_content[v_i][2]+'" class=vnewsticker'+(v_pauseOnMouseOver?" onmouseover=\'if(v_canPause&&v_count>1) {clearTimeout(v_TIM);v_cl=1}\' onmouseout=\'if(v_canPause&&v_count>1&&v_cl) v_TIM=setTimeout(\"v_canPause=0;v_slide(); v_cl=0\","+v_timeout+")\'":"")+'>':' 1) {clearTimeout(v_TIM);v_cl=1}\' onmouseout=\'if (v_canPause&&v_count>1&&v_cl) v_TIM=setTimeout(\"v_canPause=0;v_slide(); v_cl=0\","+v_timeout+")\'":"")+'>') +v_content[v_i][1]+ (v_content[v_i][0]!=''?'</a >':'</span >')+'</div >'; v_temp+='</div >';
document.write(v_temp);

it can be really hard if you want to know what HTML code your document.write() actually sends to the page. Martin Spedding wrote a nice little script for this at his blog (weblogs.asp.net/mspedding). See the following code.

<html>
<body>
<script janguage="javascript">

document.write("This is a" + "concatenation test" + "<BR>");

function newWin(){
ven = window.open('','_blank');
var results =document.documentElement.innerHTML;var match = "<";var re = new RegExp("<", "g");var newresults = results.replace(re, "&lt;");ven.document.write(newresults);}

</script>
<a href="#" onClick="javascript:newWin();">See the HTML code generated by document.write() here.</a>
</body>
</html>

Note, the function newWin() and the a href are the important pieces. Place your JavaScript code in place of the document.write line.

.

Discuss this article or this topic in our discussion forum:
(The table bellow shows a list of 8 most recent topics posted in our discussion forum. Visit our discussion forum to see more. It is possible the links below are not related to this page, but you can be certain you will find related posts in the discussion forum. You can post one yourself too.)
Email this article to a friend:
TO: 
FROM: 
2 + 6 - 3 = 
.
How can I link to this web page?

It is easy, just include the code provided below into your HTML code.

<a href="http://www.maxi-pedia.com/JavaScript+document.write+content+view" title="www.Maxi-Pedia.com: JavaScript document.write(): How to view the HTML?" target="_blank">JavaScript document.write(): How to view the HTML?</a>
.