Get encoding of page/iframe via JavaScript

痞子三分冷 提交于 2021-01-20 19:12:17

问题


I'd like to programatically determine the encoding of a page via JavaScript, or some other API from a browser. The reason I want this information is because I am attempting to fuzz major browsers on what character encodings they support, and obviously just because I sent the appropriate "Content-Type" doesn't mean that the browser will do the right thing with the encoding. Any other possible methods would be welcome, but I would rather not click "Page Info" for 50+ character encodings.


回答1:


Javascript can only report some of the things that are going on. Most browsers won't expose enough useful settings to you for you to base any hardcore tests on.

There are things such as document.inputEncoding, document.characterSet (non IE), document.charset, and document.defaultCharset (IE) which might get you some of the way there. But these might be as flaky as the actual support. That is, if a browser "thinks" it supports an encoding but really doesn't, isn't that something you want to know?

I think your best bet is to set up a dynamic test page with some fairly difficult characters on it (or a really large test set), load test in a browser, have the browser report back browser id string, encoding settings, original encoding request, and contents of testElement.innerHTML which you can then verify against expected results.




回答2:


The original answer is quite old now and support for document.characterSet has improved greatly.

>>> document.characterSet

IE 11:

"utf-8"

Firefox 52, Chrome 55 and Chrome on Android, Safari 10:

"UTF-8"

So it seems that for most purposes you can rely on this information, fixing the different case. In my case it was useful to gather this info for tracking purposes.



来源:https://stackoverflow.com/questions/318831/get-encoding-of-page-iframe-via-javascript

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