Reading from the HTML DOM returns UTF-8 characters
I have a contenteditable
div
where I'm reading individual characters and sending them off to a server (for more background this is similar to Google Wave where typing a character automatically sends it)
I was using a plain old html textfield
before and everything worked fine until I "upgraded" to a contenteditable div.
My problem is that now the characters are in UTF-8 format, which is causing some weird problems on the s开发者_运维问答erver that I would rather not debug. It would be much easier to force everything to be ASCII on the client side.
Is there any way to do this? I tried putting in a meta tag stating the html file is charset=ISO-8859-1
, but it doesnt seem to work. Reading from the div tag still returns UTF-8 codes. (One example is when I press space I get the pair 0xC2 0xA0
which corresponds to a "non-breaking white space"
Why are you using ASCII for user input? You're just delaying an enormous headache.
But, to answer your question: if your application expects ASCII, you need to check user input and convert it to ASCII manually. It sounds like you need to be checking every keystroke and converting it on-the-fly before it is even rendered to the screen. charset
doesn't apply to user input, that is dependant on system-specific settings.
But again: just use UTF-8.
精彩评论