views:

84

answers:

3
What previous result you are
referencing:(Optional) 
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"); }  

A user input some Javascript code, but the code can not be displayed as plain text within

<pre></pre>

, how to solve this problem?

A: 

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.

Zurahn
How to filter Javascript code if I accept user input?
Steven
Alas, that tag appears to have been deprecated: http://stackoverflow.com/questions/4545/xmp-tag
pavium
BuT <xmp></xmp> works so far. Is there other alternative?
Steven
HTML parsing should be done by a library in whichever server-side language you're using. In PHP there's HTML Purifier, in C# there's HTML Agility Pack.And you're right, pavium, it is deprecated, figured I'd provide a different element as an option, though.
Zurahn
A: 

You can use the <pre> and <code> tags to display formatted code. But to prevent the code from executing and not displaying you'll need to convert the text to character entities. > becomes &gt;, < becomes &lt, etc.

You could do this by using PHP, for example:

<?php echo htmlentities('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"); }'); ?>

As your system relies on user input, you might have to rely on AJAX to actually process the user input and convert it to HTML entities.

mensch
A: 

You should the your content htmlcoded just like < into &lt space &nbsp and song

Macroideal