Print html code

Asked

Viewed 1,771 times

4

How to print html code ?

Example:

<div id="css"><link rel="stylesheet" href="jquery.css.css"></div>

it does not print the link tag, it links another css, want to write this tag pro user read...

  • It is not clear in your question whether you want to do this on the client side (javascript) or server side (php for example). I leave a related link: http://answall.com/a/11058/129

3 answers

5

Use &lt; instead of <, and &gt; instead of >.

5


To do this you need to escape some special characters**, for example:

" para &quot;   
' para &apos;   
& para &amp;    
< para &lt;
> para &gt;

To facilitate you can use the function htmlentities.

The code:

<div id="css"><link rel="stylesheet" href="jquery.css.css"></div>

Equates:

&lt;div id=&quot;css&quot;&gt;&lt;link rel=&quot;stylesheet&quot;href=&quot;jquery.css.css&quot;&gt;&lt;/div&gt;

**Others may be seen here.

1

You can print special characters in HTML using HTML Entities.

If you want you can use this converter here.

Server languages - like PHP, Ruby, Python - have their own functions to convert one string into another with encoded characters.


When you will print any text that was entered by the user (such as a blog post, for example) it is recommended to use this function to prevent attacks from Javascript Injection. In this type of attack someone malicious can write tags script that will run on the user’s machine without their consent.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.