0

In my server application (on Parse Cloud Code), I want save some string data. There are HTML entities here, which I want to encode.

So i find a solution with Javascript:

var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;

This code work perfectly on html pages, where document exists. But there isn't such variable on server.

How can i declare document variable? Or maybe you know another solutions for encoding HTML entities.

2

3 Answers 3

1

You could use html-entities on Node, install it like:

npm install html-entities

then you got entities.encode(..) and entities.decode(..) functions:

var Entities = require('html-entities').XmlEntities;
entities = new Entities();
console.log(entities.encode('<>"\'&©®')); // &lt;&gt;&quot;&apos;&amp;©®

there are more examples in usage part on gihub repo.

Sign up to request clarification or add additional context in comments.

Comments

1

function encode(r){
return r.replace(/[\x26\x0A\<>'"]/g,function(r){return"&#"+r.charCodeAt(0)+";"})
}

test.value=encode('How to encode\nonly html tags &<>\'" nice & fast!');

/*************
* \x26 is &ampersand (it has to be first),
* \x0A is newline,
*************/
<textarea id=test rows=11 cols=55>www.WHAK.com</textarea>

Comments

0

Since I asked this question, I learned JavaScript and AJAX. So, my suggestion will be using AJAX and JSON for communication between browser and server-side.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.