{{ localize }}

Convert a JS string into UTF-8

Suppose you have a JS string and need to encode it into UTF-8. A good example is the twitter API that allows you to post a message. The text of the message must be added to the POST url using the 'status' parameter and the contents of that parameters must be escaped UTF-8. Use the following code to convert your text to UTF-8:

function JSStringToUTF8(aStr) {

  var converter = Components.classes[""]

  converter.charset = "utf-8" ;

  return converter.ConvertFromUnicode(aStr);