Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying HTML code

Status
Not open for further replies.

tiamat2012

Programmer
Dec 12, 2004
144
US
Hey, is there an easy way to display HTML code? I want to link to a page so that the visiters of my site can see the sourcecode.

Javascript can work, but thats a javascript link and unrecommended, and I'd rather not do it that way.

I know I can do the php way echo htmlentities($string) but I'd like to do it without getting any scripting language involved. Is there an easy way to do this?

Thank you for your time,
Kerry
 
So do you want html code displayed inside an html page or by itself? By itself, just rename .html to .txt. To display within html, just replace the element '<' symbol with &lt;. You may want to change other important and specical rendering characters, too. Also, any <p> or <br> tag you may want to put in a real linebreak element <br> after the certain tags, e.g. '&lt;br&gt;<br>'.

Here's a javascript example:
Code:
<html id="ace1">
...
<div id="ace2">
...
<script language="JavaScript"><!--
document.getElementById("ace2").innerHTML = document.getElementById("ace1").innerHTML.replace(/</g,"&lt;").replace(/>/g,"&gt;");
//--></script>

Cheers,
ND [smile]

[small]bigoldbulldog AT hotmail[/small]
 
I hadn't quite decided, but that works perfectly, thank you!

-Kerry
 
So do you want html code displayed inside an html page or by itself? By itself, just rename .html to .txt.

This doesn't always work, unfortunately. Especially not in IE, where it (very annoyingly) auto-detects the type of file from the content (even when you send a text mime type, would you believe!!!)

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hmm ok, do you have another solution? Or should I just do the javascript thing so that it works on IE as well?

-Kerry
 
If you use php you are probably familiar with *nix shell, or vi or emacs. Do the tag convert over the enitire file.

E.g.

Code:
(
echo "<html><body>"
sed '
  s/</\&lt;/g
  s/>/\&gt;/g
  s/$/<br>/
' file.html
echo "</body></html>"
) > file_txt.html

Now you've produced a real html file that has a dump of all your code in a readable and semi-formated (<br>) file.

Cheers,
ND [smile]

[small]bigoldbulldog AT hotmail[/small]
 
Actually I haven't used those, I've only used it in small work and learned on my own and haven't come in the need.

Do you know where I can learn about them (should I take this to the PHP forum?)

-Kerry

P.S. Thanks everyone for all their comments/suggestions, I'll prob be using those until I decide on the php thing.
 
A very good place to start learning about many types of web page, and other, programming is that's where I started my Javascript learning, where I enhanced my HTML knowledge, and where I (admittedly) go to check a coulple of "cheat sheets" for useful bits of code that I may have forgotten when writing replies to questions in these forums...lol


I hope this helps;
Rob Hercules
 
OK, now I've finally gotten my examples page on-line, and I included a JavaScript example page that will display its HTML source in a textbox.

To see it, go to and click on "Show source HTML code in textbox" in the left-hand nav bar.
Click the "Show Me the Source!" button in the lower right frame to see the relevant portions of the sourcecode I used. (or view source on the lower-right frame to see an example of how to parse HTML code from multiple areas of another document's source).




I hope this helps;
Rob Hercules
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top