Displaying HTML Code Blocks
<p>I am trying to make a site that has code examples (in HTML,JavaScript,CSS,etc) and need to figure out how to display the code without the browser interpreting it as anything but text.</p>

<p>I wrote a JavaScript function to convert the &lt;, > from the text, and then it writes the result, but the browser still seems to recognize &lt; /script > (without the spaces before and after /script.</p>

<pre><code>        function codeToHTML(input) {
            var output = "";
            for(i=0;i&lt;input.length;i++) {
                var c = input.charAt(i);
                if(c=='&lt;') {
                    output+="&amp;lt;";
                } else if (c=='&gt;') {
                    output+="&amp;gt;"; 
                } else if (c=='\n') {
                    output+="&lt;br&gt;";
                } else if (c==' ') {
                    output+="&amp;nbsp;";
                } else {
                    output+=c;
                }
            }
            return output;
        }
</code></pre>

<p>Is there a better way to approach this?</p>
