Advertisement
baptx

htmlentities.js

Aug 7th, 2016
1,664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // HTML entities Encode/Decode
  2.  
  3. function htmlspecialchars(str) {
  4.     var map = {
  5.         "&": "&",
  6.         "<": "&lt;",
  7.         ">": "&gt;",
  8.         "\"": "&quot;",
  9.         "'": "&#39;" // ' -> &apos; for XML only
  10.     };
  11.     return str.replace(/[&<>"']/g, function(m) { return map[m]; });
  12. }
  13. function htmlspecialchars_decode(str) {
  14.     var map = {
  15.         "&amp;": "&",
  16.         "&lt;": "<",
  17.         "&gt;": ">",
  18.         "&quot;": "\"",
  19.         "&#39;": "'"
  20.     };
  21.     return str.replace(/(&amp;|&lt;|&gt;|&quot;|&#39;)/g, function(m) { return map[m]; });
  22. }
  23. function htmlentities(str) {
  24.     var textarea = document.createElement("textarea");
  25.     textarea.innerHTML = str;
  26.     return textarea.innerHTML;
  27. }
  28. function htmlentities_decode(str) {
  29.     var textarea = document.createElement("textarea");
  30.     textarea.innerHTML = str;
  31.     return textarea.value;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement