Rafal Rafal - 22 days ago 6
Javascript Question

How can I load a .txt file containing html tags without interpreting it by html?

When i load a .txt file containing html marks using FileReader() method and I try display it on the screen, it show me already interpreted code. Here is a problem, because I need these tags too, without interpreting it by html. For example:
instead of:

Some text in file.txt

I want to get:
 
<p>
Such text in file.txt with these html tags
</p>


Here a code what I'm using: https://jsfiddle.net/fkwugnsn/. Try now load a txt file with the content of html. You see that it's already interpreted by html. And I need no interpreted code.

Answer

You can use this function for this:

function htmlEntities(str) {
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

This function will replace &, <, > and " with html entities.

source