Is there a javascript equivalent of htmlencode / htmldecode from asp.net?

The problem is this:

You have a text field, you enter text, send it to the server. On another page, this value is retrieved and displayed on the screen in the text box and on the shortcut.

It is important to stop script attacks, and asp.net will not allow you to send unsafe code, so send you a javascript <with &lt; and the same for>

When the values ​​are received from the server, they will return with &lt; and &gt; that are suitable for displaying on the label, but when they are placed in the text box, they should be replaced by <and>

Data must be stored securely in the database, as other users can use this content. From a security point of view, I would like to call htmlencode on it, and then save it. It is this encoded html that I would like to display on the label on the client, but the decoded version that I would like to display in the text box.

So what I need is a htmldecode solution in javascript. htmlencode / decode replaces more than just <> and without a final list, I cannot create my own method. Is there a solution there?

+4
source share
1 answer

Instead of trying to turn a line of text into HTML, then add it to the document using innerHTML; use standard DOM methods.

 myElement.appendChild( document.createTextNode(myString) ); 
+4
source

All Articles