ability to set encoding for ajax sent data. working solution

ability to set encoding for ajax sent data. working solution


hello friends,

i'm in love with the jquery way to control the browser.
everything may be done quite elegantly, but i wish to ask for yet another parameter to ajax, to cope to non unicode encodings on the server side:

a widespread misfeature of all the browsers i know of is not respecting the page encoding when sending data through ajax (while they do respect it when submitting a form). i propose to allow the programmer to choose the encoding, and give my code as example.

function unicode_escape is being used as a replacement to avoid usage of encodeURIComponent.

here's an example usage of my code:

               (function($){
                       var _ajax = $.ajax;
                       $.extend({
                               ajax: function(options){
                                       var data = new String("");
                                       for(var item in options.data){
                                               if (data != "") {
                                                       data += "&";
                                               }
                                               data += unicode_escape(String(item), 'windows-1255') + '=';
                                               data += unicode_escape(String(options.data[item]), 'windows-1255');
                                       }
                                       options.data = data;
                                       return _ajax.call(this, options);
                               }
                       });
               })(jQuery);
instead of this code, i wish to ask you to provide a way to choose an alternate escaping function to be used instead of encodeURIComponent in the ajax settings and parameters.

here i choose sending the data encoded as windows-1255, the encoding provided in my attached file, but if this parameter is missed, the current page encoding will be choosen.
if the encoding is not provided, encodeURIComponent will be used.
the data for most encodings is not so big, and a mechanism should be put up to load the data on demand, like one file for each encoding or family thereof, that would update the encodings dictionary on their load.

i'm open to comments, thanks a lot for your attention

here's an example for the conversion function and table:

var unicode_escape_maps = {
  'windows-1255': {
    0x20AC:'80', //EURO SIGN
    0x201A:'82', //SINGLE LOW-9 QUOTATION MARK
    0x0192:'83', //LATIN SMALL LETTER F WITH HOOK
    0x201E:'84', //DOUBLE LOW-9 QUOTATION MARK
    0x2026:'85', //HORIZONTAL ELLIPSIS
    0x2020:'86', //DAGGER
    0x2021:'87', //DOUBLE DAGGER
    0x02C6:'88', //MODIFIER LETTER CIRCUMFLEX ACCENT
    0x2030:'89', //PER MILLE SIGN
    0x2039:'8B', //SINGLE LEFT-POINTING ANGLE QUOTATION MARK
    0x2018:'91', //LEFT SINGLE QUOTATION MARK
    0x2019:'92', //RIGHT SINGLE QUOTATION MARK
    0x201C:'93', //LEFT DOUBLE QUOTATION MARK
    0x201D:'94', //RIGHT DOUBLE QUOTATION MARK
    0x2022:'95', //BULLET
    0x2013:'96', //EN DASH
    0x2014:'97', //EM DASH
    0x02DC:'98', //SMALL TILDE
    0x2122:'99', //TRADE MARK SIGN
    0x203A:'9B', //SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
    0x20AA:'A4', //NEW SHEQEL SIGN
    0x00D7:'AA', //MULTIPLICATION SIGN
    0x00F7:'BA', //DIVISION SIGN
    0x05B0:'C0', //HEBREW POINT SHEVA
    0x05B1:'C1', //HEBREW POINT HATAF SEGOL
    0x05B2:'C2', //HEBREW POINT HATAF PATAH
    0x05B3:'C3', //HEBREW POINT HATAF QAMATS
    0x05B4:'C4', //HEBREW POINT HIRIQ
    0x05B5:'C5', //HEBREW POINT TSERE
    0x05B6:'C6', //HEBREW POINT SEGOL
    0x05B7:'C7', //HEBREW POINT PATAH
    0x05B8:'C8', //HEBREW POINT QAMATS
    0x05B9:'C9', //HEBREW POINT HOLAM
    0x05BB:'CB', //HEBREW POINT QUBUTS
    0x05BC:'CC', //HEBREW POINT DAGESH OR MAPIQ
    0x05BD:'CD', //HEBREW POINT METEG
    0x05BE:'CE', //HEBREW PUNCTUATION MAQAF
    0x05BF:'CF', //HEBREW POINT RAFE
    0x05C0:'D0', //HEBREW PUNCTUATION PASEQ
    0x05C1:'D1', //HEBREW POINT SHIN DOT
    0x05C2:'D2', //HEBREW POINT SIN DOT
    0x05C3:'D3', //HEBREW PUNCTUATION SOF PASUQ
    0x05F0:'D4', //HEBREW LIGATURE YIDDISH DOUBLE VAV
    0x05F1:'D5', //HEBREW LIGATURE YIDDISH VAV YOD
    0x05F2:'D6', //HEBREW LIGATURE YIDDISH DOUBLE YOD
    0x05F3:'D7', //HEBREW PUNCTUATION GERESH
    0x05F4:'D8', //HEBREW PUNCTUATION GERSHAYIM
    0x05D0:'E0', //HEBREW LETTER ALEF
    0x05D1:'E1', //HEBREW LETTER BET
    0x05D2:'E2', //HEBREW LETTER GIMEL
    0x05D3:'E3', //HEBREW LETTER DALET
    0x05D4:'E4', //HEBREW LETTER HE
    0x05D5:'E5', //HEBREW LETTER VAV
    0x05D6:'E6', //HEBREW LETTER ZAYIN
    0x05D7:'E7', //HEBREW LETTER HET
    0x05D8:'E8', //HEBREW LETTER TET
    0x05D9:'E9', //HEBREW LETTER YOD
    0x05DA:'EA', //HEBREW LETTER FINAL KAF
    0x05DB:'EB', //HEBREW LETTER KAF
    0x05DC:'EC', //HEBREW LETTER LAMED
    0x05DD:'ED', //HEBREW LETTER FINAL MEM
    0x05DE:'EE', //HEBREW LETTER MEM
    0x05DF:'EF', //HEBREW LETTER FINAL NUN
    0x05E0:'F0', //HEBREW LETTER NUN
    0x05E1:'F1', //HEBREW LETTER SAMEKH
    0x05E2:'F2', //HEBREW LETTER AYIN
    0x05E3:'F3', //HEBREW LETTER FINAL PE
    0x05E4:'F4', //HEBREW LETTER PE
    0x05E5:'F5', //HEBREW LETTER FINAL TSADI
    0x05E6:'F6', //HEBREW LETTER TSADI
    0x05E7:'F7', //HEBREW LETTER QOF
    0x05E8:'F8', //HEBREW LETTER RESH
    0x05E9:'F9', //HEBREW LETTER SHIN
    0x05EA:'FA', //HEBREW LETTER TAV
    0x200E:'FD', //LEFT-TO-RIGHT MARK
    0x200F:'FE'  //RIGHT-TO-LEFT MARK
  }
};

var charset_re = new RegExp('.*?charset\s*=\s*([a-z0-9-]+)', 'i');


function unicode_escape(str, charset) {
    if (typeof(charset) == 'undefined') {
        var content = $("meta").attr("content");
        var charset = content.replace(charset_re, '$1');
        charset = charset.toLowerCase();
    }
    var result = '';
    if (charset in unicode_escape_maps) {
        var code = 0, c = 0;
        for (c = 0; c < str.length; ++c) {
            code = str.charCodeAt(c);
            if (code in unicode_escape_maps[charset]) {
                result += '%' + unicode_escape_maps[charset][code];
            }
            else {
                result += escape(str.substr(c, 1));
            }
        }
    }
    else {
        result = encodeURIComponent(str);
    }
    return result;
}

--
alex