Javascript browser charset rendering detection I am attempting to polyfill the broken character encoding system in javascript. For example, characters such as , have a length of 2 and are treated as 2 separate characters in regular expressions when they should have a length of 1 and be treated as a single character in regular expressions. This may not seem important, but I don't want to have a programming nightmare when I deploy my site multilingually with asian and african languages. I am attempting to polyfill the whole thing, but since I can't find any articles, this is the best I can do. I am attempting to detect the encoding based on the size of different characters since that is what I am, in essence, doing: polyfilling the size of the characters.



&lt;script&gt;
 // Charset Polyfiller
 let   charsettransfer = Object.create(null),
        maxUniChar = String.fromCodePoint(0x10ffff);
  let   needsPolyfill = true;
  
  if ('\xE2'.length === '\xE1'.length*2){
    charsettransfer.to = 'GB 18030';
    if ('\xE1'.length === 2){
      charsettransfer.from = 'ascii';
    } else if ('\xE1'.length === 1) {
      charsettransfer.from = 'UCS-16';
    }
  } else if ('\x7f'.length === 1 &amp;&amp; '\x80'.length === 2){
    charsettransfer.from = 'ascii';
    charsettransfer.to = 'utf-7';
  } else if (maxUniChar.length === 4){
    charsettransfer.from = 'ascii';
    if ('\0'.length &lt;= 1){
      charsettransfer.to = 'utf-8';
    } else if ('\u9999'.length === 3 &amp;&amp; '\0'.length === 3){
      charsettransfer.to = 'utf-20';
    } else if ('\u9999'.length === 4) {
      charsettransfer.to = 'utf-32';
    } else {
      charsettransfer.to = 'utf-16';
    } 
  } else if (maxUniChar.length === 3) {
    charsettransfer.from = 'ascii';
    charsettransfer.to = 'utf-24';
  // utf-24 is my own nickname for when a browser uses only the minimal 
  // 24 bits for utf-32 (like python)
  } else if (maxUniChar.length === 2) {
    charsettransfer.from = 'ucs-16';
    charsettransfer.to = 'utf-16';
  } else if (maxUniChar.length === 1) {
    charsettransfer.from = 'ucs-32';
    charsettransfer.to = 'utf-32';
    needsPolyfill = false;
  }
  
 /*if ('\u9999'.length === 3){
  charsettransfer.from = 'ascii';
  charsettransfer.to = 'utf-8';
 } else if ('\0'.length === 4) {
  charsettransfer.to = 'utf-32';
    charsettransfer.from = 'ascii';
 } else if ('\0'.length === 3) {
  charsettransfer.to = 'utf-24';
  charsettransfer.from = 'ascii';
 } else if ('\0'.length === 2) {
  charsettransfer.to = 'utf-16';
  // utf-24 is my own nickname for when a browser uses only the minimal 
  // 24 bits for utf-32 (like python)
  charsettransfer.from = 'ascii';
 } else if ('\uD83D\uDE12'.length === 1){
  charsettransfer.from = 'ucs-32';
  charsettransfer.to = 'utf-32';
 }*/
  
  document.write('Your browser is attempting to render ' + charsettransfer.to
       + ' in ' + charsettransfer.from + '.\&lt;br /&gt; Your browser needing the polyfill is ' + needsPolyfill + '.');
&lt;/script&gt;




How did I do? Am I on the right track? Are there any things I could add? Any improvements I could make? Any bugs that should be fixed? (sorry if i'm ranting, but i'm really concerned about this segment of code because LOTS of days of work are gonna depend on it, and I can't seem to find any helpful articles reguarding it).
Just don't. Standard practice is to use UTF-8 everywhere possible, and get rid of all other encodings. Especially if you have a multilingual system, dealing with multiple legacy encodings is a huge headache — just like this code above.