Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show literal HTML script in a web page?

Tags:

html

I have some HTML script that I would like to make it display in a web page like the code option here:

<a href="#">test</a>

How can I do that?

like image 385
pmerino Avatar asked Jul 20 '11 13:07

pmerino


People also ask

How do I display HTML code without executing?

Use HTML Special Character Codes var myCode = "<b>This is not bold</b>"; $('span#code-span'). text(myCode); Using text instead of html will cause tags to be rendered exposed instead of being executed.

How do I display code snippets in HTML?

Note: <pre> tag is used to display code snippets because it always keeps the text formatting as it.


2 Answers

You need to HTML-escape your text, as you can easily tell if you look at the HTML source of this very page:

<div class="post-text">
    <p>I have some HTML that I would like to make it appear like the code option here</p>

    <pre><code>&lt;a href="#"&gt;test&lt;/a&gt;
    </code></pre>

    <p>How can I do it?</p>
</div>            
like image 154
SLaks Avatar answered Sep 20 '22 15:09

SLaks


You'll need to encode the angle brackets etc in order to show this on the page.

You can do this manually in html as:

&lt;a href="#"&gt;test&lt;/a&gt;

Where:

&lt; = <

&gt; = >

For other characters here's a table of character codes: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

like image 34
Jamie Dixon Avatar answered Sep 20 '22 15:09

Jamie Dixon