Posted By

mifly on 04/11/09


Tagged

escapeHTML


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Vordreller
fadlee


(un)escapeHTML


 / Published in: JavaScript
 

URL: http://www.gracecode.com/archives/2873/

  1. escapeHTML: function(str) {
  2. var div = document.createElement('div');
  3. var text = document.createTextNode(str);
  4. div.appendChild(text);
  5. return div.innerHTML;
  6. }
  7.  
  8. unescapeHTML: function(str) {
  9. var div = document.createElement('div');
  10. div.innerHTML = str.replace(/<\/?[^>]+>/gi, '');
  11. return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
  12. }

Report this snippet  

You need to login to post a comment.