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.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
The original answer is quite old now and support for
document.characterSet
has improved greatly.IE 11:
Firefox 52, Chrome 55 and Chrome on Android, Safari 10:
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.
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
, anddocument.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.