Today I wanted to use the htmlentities-function in JavaScript. This useful function is available for PHP but it’s not for JavaScript. The reason why I needed the htmlentities-function was the use of the element property innerHTML. I wanted to insert contents into a div container and output some JavaScript variables. The problem: As soon as any user inserts a special character, like ‘, “, >, < or & and so on, the operation fails of course. First I thought of encoding the variables by the server with an ajax request but I did not want to use up so much traffic. So I came up with this tiny function that works similar to htmlentities but encodes every character, also normal ASCII characters. It does not really encode the characters to html entites but to xml entities which is fine for its use in HTML.
Here is the function:
<script type="text/javascript"> function htmlentities(input) { var code=""; for(i=0; i < input.length; i++) { code=code+"&#"+input.charCodeAt(i)+";"; } return code; } </script>
If you want to test it, you can do it here:
Input
Raw Output
Output with encoded entities (you will not see any change compared to the input)
This is the code for the code playground above. As you can see it is very simple.
Input <textarea id="input" rows="3" cols="60">special characters: <>”&’ more special characters: ♫♪ ▼▲▼▲ normal characters: abc123</textarea> <input onclick="javascript:document.getElementById('output').value=htmlentities(document.getElementById('input').value); document.getElementById('enc_output').innerHTML=htmlentities(document.getElementById('input').value);" type="button" value="Run htmlentities()" /> Raw Output <textarea id="output" rows="3" cols="60"></textarea> Output with encoded entities (you will not see any change compared to the input) <textarea id="enc_output" rows="3" cols="60"></textarea>
« HTML code box for formatted code “This is not a valid Gadget Package.” – Zipping a Windows Sidebar Gadget »

Nice article, I do this with a free webtool, http://www.html-entities.org/
I wanted to insert contents into a div container and output some JavaScript variables. First I thought of encoding the variables by the server with an ajax request but I did not want to use up so much traffic. It does not really encode the characters to html entites but to xml entities which is fine for its use in HTML. As you can see it is very simple.Extremely helpful information specially the last part I care for such information much. I was seeking this certain information for a long time. I do consider all the ideas you’ve introduced for your post. They are really convincing and will definitely work. Still, the posts are very quick for starters. Could you please prolong them a little from next time? Thank you for the post.
Dear Web Designer,
thank you very much for your comment. I really appreciate your comment. Next post, I will try to write my posts more carefully to be understood by starters as well, but I think it is not that simple to prepare blog posts adequately for all audiences
Best regards from Germany
Birk