Printing html in a browser
Anyone have any bright ideas about this one? One of our placement students is developing some javascript quizzes that are built via perl based on input from a user. They are aksed how many questions in the quiz, the questions details etc and when they click submit, perl builds these javascript games and outputs the raw code to a textarea so the user can cut and paste the code into their own webpage. This works fine, until the latest widget outputs textareas, so if the script displays html into the textarea, the brower closes the textarea at the first </textarea> tag. She has asked me how to display html/javascript code to a webpage without rendering it, and I am stumped. I thought about all those sites which display code. but all the ones I have checked use textareas.
I was hoping for a stylesheet tag such as <div class = "dontrenderme">. Or by using <code> tags, or <pre> tags but none of those work consistently.
How we fixed it. We firstly tried printing a content type text/plain, but IE rendered the text as html anyway:
Here's the problem and it's blinking annoying:
http://ppewww.ph.gla.ac.uk/~flavell/www/content-type.html
Note that Internet Explorer incorrectly ignores server-provided MIME types, so it sometimes "does the right thing" when the server is misconfigured. Other browsers correctly heed the server-provided MIME types, so they will reveal server misconfigurations.
Solution
Thanks to the comments section and a stroke of luck that today's Perl Advent Calendar entry was HTML::Entity. If you convert the whole string to numeric entities you can print the html straight out to the browser in a textarea but probably to the html page, I have only checked this in IE 6.0 and Netscape 4.8.
Comments
We have just done the lazy thing for the time being to get it working for her project and converted all closing textareas to ampersandlt and ampersand gt.
Posted by: Andy | December 1, 2003 12:25 PM
Andy, that's the way to do it. Convert all < and > to their entity equivalents. I am nearly sure all browsers will render them in a textarea correctly. PHP has a function htmlentities() which does this, and there should be many PERL equivs to do the same.
Posted by: Gareth | December 1, 2003 07:55 PM
Cheers Gareth, I'll give that a shot in the morning.
Posted by: jaffs | December 1, 2003 09:02 PM