Reputation: 9011
So I have a web page that is encoded in ISO-8859 as per the HTTP header that returns its content. I return text content 😳😳😳😳 and as expected it is mangled by the ISO encoding.
However when I make a JSONP AJAX call that returns the same text content 😳😳😳😳, and insert it onto the page, those emoticons are displayed correctly! Notably the AJAX call lacks an encoding type header as its Content-Type header is simply text/javascript.
What's going on here? Does my Chrome browser do some sort of clever tricks to make sure the AJAXed content is displayed correctly?
In case it matters I am using the jQuery library to do AJAX calls.
Upvotes: 0
Views: 331
Reputation: 203519
This is mostly conjecture, as I haven't looked into it extensively, but from what I understand is that browsers convert HTML textual content to some internal representation (perhaps UTF-16, I don't know). I found a mention of that here.
They do that using the clues that the server (Content-Type
header), the HTML itself (<meta charset=...
, etc) or the user (by explicitly setting the encoding for a page) provides.
Any new textual content that gets pushed into the DOM, for instance that which is retrieved using an AJAX request, undergoes the same transcoding. In other words, the browser doesn't use the encoding of the rest of the page (or perhaps only in the case where it has absolutely no idea when the encoding is), but the encoding that's provided by the server, in the AJAX response (or it uses a default if the server doesn't set it).
So internally, everything gets converted to the same encoding, which is why you can inject UTF-8-encoded data using JS into an ISO-8859-encoded page and it works.
Upvotes: 1