<xmp> with innerHTML to display source code

D

ddailey

The code which follows is something I cobbled together quite hastily
to display to students a way of illustrating HTML source code
(including CSS and JavaScript) alongside a page as rendered. I've done
this a dozen different ways for slightly different purposes and rather
can't remember what the various outcomes of my inquiries have been.

In the following I use <xmp> as a way of displaying source code. It
works in IE, Opera, and FF, but I recall hearing that <xmp> is to be
deprecated and that it provides no functionality that cannot otherwise
be performed without it. Whence the question: how does one do the
following without <xmp>? [aa identical approach using <code> seems to
do utterly zilch]

thanks in advance
dpd

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><style>
#left{
position: absolute;
top: 0;
left: 0;
width: 55%;
background: #fe8;
color: #018;
}
#right{
position: absolute;
top: 0;
left: 55%;
}
..text{
margin: 15px;
}
</style>
<script>
var a = document.documentElement.innerHTML

function display(L){
L.innerHTML+="<code>"+a+"<\/code>"
}
</script></head>
<body onload="display(document.getElementById('left'))">
<div id="left">
<div class="text">
<b>Here, in the div with id="left", we see the mechanics of the page.</
b>
</div>
</div>
<div id="right">
<div class="text">
<h2>Two column layout using CSS</h2>
<p>Here in the div with id="right", is where ordinary web stuff goes.</
p>
</div>
</div>

</body>
</html>
 
U

Une Bévue

....

I'm using :
<html><head><script>
window.onload=function(){
document.getElementById('source').appendChild(
document.createTextNode(
document.getElementsByTagName('script')[0].text));
}
</script></head>
<body>
<dl>
<dt>Source code:</dt>
<dd>
<pre id="source"/>
</dd>
</dl>
</body></html>

tested on Mac OS X with Firefox, Opera and Safari.
 
L

Lee

...

I'm using :
<html><head><script>
window.onload=function(){
  document.getElementById('source').appendChild(
    document.createTextNode(
      document.getElementsByTagName('script')[0].text));}

</script></head>
<body>
  <dl>
    <dt>Source code:</dt>
    <dd>
      <pre id="source"/>
    </dd>
  </dl>
</body></html>

tested on Mac OS X with Firefox, Opera and Safari.

Try textarea instead of the code tags.
 
L

Lee

...

I'm using :
<html><head><script>
window.onload=function(){
  document.getElementById('source').appendChild(
    document.createTextNode(
      document.getElementsByTagName('script')[0].text));}

</script></head>
<body>
  <dl>
    <dt>Source code:</dt>
    <dd>
      <pre id="source"/>
    </dd>
  </dl>
</body></html>

tested on Mac OS X with Firefox, Opera and Safari.

Try the textarea tag instead of the code tag.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top