Html – How to display code in plain text


I want to display bare code on an HTML page, I tried this:

function getSize() {
    var myFSO = new ActiveXObject("Scripting.FileSystemObject");
    var filepath = document.upload.file.value;
    var thefile = myFSO.getFile(filepath);
    var size = thefile.size;
    alert(size + " bytes");

The above JavaScript code is some code entered by the user. I can't figure out to show this bare code on the html page without being interpreted and screwed up by the browser.

How do I display bare code on an HTML page?

Best Solution

I'm not quite clear on the specifics of the issue, as pre tags should, in general, do the trick, but here's an alternative tag:

<xmp>[Code can be displayed here]</xmp>

If you're using a server-side language, though, I'd suggest converting to HTML entities before outputting, then using CSS to style it.

As well, be sure if you're accepting user input that any JavaScript is being filtered and never executed.