David
David

Reputation: 16736

Replace Greek letters in Javascript

I've got the function below which replaces greek letters with a similar 'normal' letter. However, a few browsers don't really like this code (IE). I imagine its because of the greek letters in the code.

How can I do this without breaking js?

function string_to_url(string) {
    replace = new Array('á','Á','é','É','í','Í','ó','Ó','ú','Ú','ü','Ü','ö','Ö','õ','Õ','û','Û','¾','š','è','ž','ý','ô','ä','ò','å','¼','Š','È','Ž','Ý','Ò','Å','ì','Ì','ê','Æ','æ','Ø','ø');

    replace_n = new Array('a','A','e','E','i','I','o','O', 'u','U','u','U','o','O','o','O','u','U','l','s','c','z','y','o','a','n','a','l','s','C','Z','Y','N','A','e','E','e','AE', 'ae','O','o');

    for (var i = 0; i < replace.length; i++) {
        string = string.replace(replace[i], replace_n[i]);
    }




    return string;
}

Upvotes: 3

Views: 3392

Answers (1)

Christophe L
Christophe L

Reputation: 14035

You can try to unicode encode the non-standard chars in your JS see if that works better with a "pure-ASCII" source:

function string_to_url(string) {
    var replace = new Array('\u00E1','\u00C1','\u00E9','\u00C9','\u00ED','\u00CD','\u00F3','\u00D3','\u00FA','\u00DA','\u00FC','\u00DC','\u00F6','\u00D6','\u00F5','\u00D5','\u00FB','\u00DB','\u00BE','\u0161','\u00E8','\u017E','\u00FD','\u00F4','\u00E4','\u00F2','\u00E5','\u00BC','\u0160','\u00C8','\u017D','\u00DD','\u00D2','\u00C5','\u00EC','\u00CC','\u00EA','\u00C6','\u00E6','\u00D8','\u00F8');
    var replace_n = new Array('a','A','e','E','i','I','o','O', 'u','U','u','U','o','O','o','O','u','U','l','s','c','z','y','o','a','n','a','l','s','C','Z','Y','N','A','e','E','e','AE', 'ae','O','o');

    for (var i = 0; i < replace.length; i++) {
        string = string.replace(replace[i], replace_n[i]);
    }

    return string;
}

(I did the conversion with http://rishida.net/tools/conversion/).

Upvotes: 3

Related Questions