User:Glazou

  • Revision slug: User:Glazou
  • Revision title: User:Glazou
  • Revision id: 139899
  • Created:
  • Creator: Glazou
  • Is current revision? Yes
  • Comment 129 words added

Revision Content

{{ localize('System.API.new-user-page-text') }}

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["@mozilla.org/intl/scriptableunicodeconverter"]
                    .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);

  converter.charset = "utf-8" ;

  return converter.ConvertFromUnicode(aStr);
}

 

Revision Source

<p>{{ localize('System.API.new-user-page-text') }}</p>
<h3 id="Convert_a_JS_string_into_UTF-8">Convert a JS string into UTF-8</h3>
<p>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:</p>
<pre>function JSStringToUTF8(aStr) {

  var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
                    .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);

  converter.charset = "utf-8" ;

  return converter.ConvertFromUnicode(aStr);
}
</pre>
<p> </p>
Revert to this revision