Reading UTF8 characters using innerHTML returns 0xfffd for all characters

I am reading an HTML document containing UTF-8 characters, but when I access the document, all the "bad" characters are displayed as . I tried this in all major browsers and it behaves the same. When I , it shows these symbols as "diamond with a label?". innerHTML0xfffdalert() innerHTML

Surprisingly, the following works fine by correctly displaying the UTF-8 char in the warning window, so it doesn't work alert().

alert("Doppelg\u00e4nger!");

Why can't I access UTF-8 characters with ? Or there is another way to access them in JavaScript. innerHTML

0
source share
2

, .

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

javascript:

var metaTags = document.getElementsByTagName("META");

, . utf-8 ISO-8859-1:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

htmlEncode HTML. :

function encodeHTML(str){
 var aStr = str.split(''),
     i = aStr.length,
     aRet = [];

   while (--i) {
    var iC = aStr[i].charCodeAt();
    if (iC < 65 || iC > 127 || (iC>90 && iC<97)) {
      aRet.push('&#'+iC+';');
    } else {
      aRet.push(aStr[i]);
    }
  }
 return aRet.reverse().join('');
}

, , [a-zA-Z]. Doppelgänger Doppelg & # 228; nger, .

+2

UTF-8? .innerHTML UTF-8.

0

Source: https://habr.com/ru/post/1609139/


All Articles