1

what the built-in or user-defined function that I can use in Javascript or jQuery to convert from one character encoding to another?

For Example, FROM "utf-8" TO "windows-1256" OR FROM "windows-1256" TO "utf-8"

A practical use of that is if you have a php page with specific character encoding like "windows-1256" that you could not change it according to the business needs and when you use ajax to send a block data from database using json which uses "utf-8" encoding only so you need to convert the ouput of json to this encoding so that the characters and the strings will be displayed well

Thanks in advance .....

4
  • JavaScript is generally very weak on this. What is your use case, what do you need to do? Commented Sep 29, 2011 at 9:58
  • I have edited my question to provide a practical case for that. Commented Sep 29, 2011 at 10:13
  • I'm not 100% sure right now and I don't have the time to test, but I was always under the impression that the browser handles that scenario and converts the data automatically? Commented Sep 29, 2011 at 10:24
  • Mr. Pekka Thanks for you comments but you mean the browser will convert it automatically without the need to enforce specific character encoding conversion but in this case I face the problem of getting unknown and unreadable characters due to this unwanted auto conversion and this is what i am asking about. Commented Sep 29, 2011 at 11:58

1 Answer 1

1

From the standpoint of a JavaScript runtime environment, there's really no such thing as character encodings – the messiness of encodings is abstracted away from you. By spec, all JS source text is interpreted as Unicode characters, and all Strings are Unicode.

As such, there's no way in JavaScript to represent characters in anything other than Unicode. Look at the methods available on a String instance – you'll see there's nothing related to character encoding.

Because JavaScript runs in Unicode, and all JavaScript strings are stored in Unicode, all AJAX calls will be transmitted over the wire in Unicode. From the jQuery AJAX docs:

Data will always be transmitted to the server using UTF-8 charset; you must decode this appropriately on the server side.

Your PHP script is going to have to cope with Unicode input from AJAX calls.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your logical answer although sometimes the business rules may make you use a different encoding that copes with the database that is distributed and the encoding changes may affect all the applications and many text may not be read (especially Arabic), Anyhow thanks for your useful information.
Because JavaScript runs in Unicode, and all JavaScript strings are stored in Unicode, all AJAX calls will be transmitted over the wire in Unicode. So if I use pure JavaScript without any AJAX call, instead use just a submit form option will that also send the data in Unicode form? Is this related to this?: stackoverflow.com/questions/22179247/…
@ChankeyPathak: No, not related. A <form>'s submission by the browser is a different beast. The character encoding that a browser uses when submitting a form element can vary based on browser, the document's encoding, the <form accept-charset> attribute, and even the presence of characters not representable in the encoding that would be otherwise selected. HTML5 specifies how browsers should select an encoding, but old browsers don't follow this spec.
OK, but in my case it all worked fine before, when I started using jQuery.post I got wrong data in server side, any idea on how can I force the jQuery post method to send the data as it is?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.