Advertisement
krames12

html character code function

Sep 20th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // check character names for special characters
  2. function htmlEncode(characterName) {
  3.   var n = characterName.length;
  4.   var encoded = [];
  5.  
  6.   while (n--) {
  7.     var charCode = characterName[n].charCodeAt();
  8.     if (charCode < 65 || charCode > 127 || (charCode > 90 && charCode < 96)) {
  9.       encoded[n] = '&#' + charCode + ';';
  10.     } else {
  11.       encoded[n] = characterName[n];
  12.     }
  13.   }
  14.  
  15.   return encoded.join('');
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement