How to verify browser support UTF-8 characters properly?

巧了我就是萌 提交于 2019-12-07 12:00:55

问题


Is there a way to identify whether the browser encoding is set to/supports "UTF-8" from Javascript?

I want to send "UTF-8" or "English" letters based on browser setting transparently (i.e. without asking the User)

Edit: Sorry I was not very clear on the question. In a Browser the encoding is normally specified as Auto-Detect (or) Western (Windows/ISO-9959-1) (or) Unicode (UTF-8). If the user has set the default to Western then the characters I send are not readable. In this situation I want to inform the user to either set the encoding to "Auto Detect" (or) "UTF-8".


回答1:


First off, UTF-8 is an encoding of the Unicode character set. English is a language. I assume you mean 'ASCII' (a character set and its encoding) instead of English.

Second, ASCII and UTF-8 overlap; any ASCII character is sent as exactly the same bits when sent as UTF-8. I'm pretty sure all modern browsers support UTF-8, and those that don't will probably just treat it as latin1 or cp1252 (both of which overlap ASCII) so it'll still work.

In other words, I wouldn't worry about it.

Just make sure to properly mark your documents as UTF-8, either in the HTTP headers or the meta tags.




回答2:


I assume the length of the output (that you read back after outputting it) can tell you what happened (or, without JavaScript, use the Accept-Charset HTTP header, and assume the UTF-8 encoding is supported when Unicode is accepted).

But you'd better worry about sending the correct UTF-8 headers et cetera, and fallback scenarios for accessibility, rather than worrying about the current browsers' UTF-8 capabilities.



来源:https://stackoverflow.com/questions/734534/how-to-verify-browser-support-utf-8-characters-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!