Full answer
Garbled text like é instead of é is called mojibake. It happens when text is saved in one encoding and read in another. The usual case is a file saved as UTF-8 that the browser reads as a single-byte encoding such as Windows-1252. In UTF-8 the letter é takes two bytes. Read as Windows-1252, each byte is a separate character, which gives à and ©.
The browser picks the encoding in this order:
- A byte order mark at the start of the file.
- The
charsetparameter of theContent-Typeheader sent by the server. - A
meta charsettag in the first part of the file (the first 1024 bytes). - A guess, which can be wrong.
The header wins over the meta tag. So a server that sends text/html; charset=iso-8859-1 overrides a correct meta charset="utf-8", and a missing header with a meta tag placed late in the head can also fail.
Debug steps:
- Open the network panel and look at the
Content-Typeheader of the page. - In the page, read
document.characterSetto see what the browser decided. - Check how the file is saved in the editor, and fix the setting.
- Check data that comes from a database, an API or a form: each hop must use UTF-8, including the connection and the table collation.
- Put
<meta charset="utf-8">first in the head, and make the server send UTF-8 in the header.
The fix is always to make every part agree, not to replace the characters with entities.
The letter é is stored as the two bytes 195 and 169. Decoded as Windows-1252, they give é. Decoded as UTF-8, they give é. The page itself is read as UTF-8.
Repeated damage cannot be undone by changing the encoding, so fix the source and the data.
<p id="out"></p>
<script>
const bytes = new TextEncoder().encode("é");
const wrong = new TextDecoder("windows-1252").decode(bytes);
const right = new TextDecoder("utf-8").decode(bytes);
window.result = { bytes: Array.from(bytes), wrong, right, set: document.characterSet };
out.textContent = wrong + " " + right;
</script>The garbled text was produced on purpose by decoding bytes with the wrong encoding. The test did not use a real server or real response headers. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- UTF-8 read as a single-byte encoding gives characters like é.
- Order: byte order mark, header, meta charset, guess.
- The header overrides the meta tag.
- Check document.characterSet and the network panel.
- Every hop of the data must use UTF-8.
Common mistakes
- Adding a meta charset tag when the server header says something else.
- Fixing the display with entities and leaving wrong bytes in the database.
- Placing the meta charset tag far down the head.
Follow-ups
- Which wins, the Content-Type header or the meta tag?
- How can a script read the encoding the browser used?
Tips
- Show the two bytes example for é, and the order the browser uses to decide.
- Say that the header wins over the meta tag.