Forum Moderators: open

Message Too Old, No Replies

Using Special Characters (non-US-ASCII) with Javascript

Special characters

         

music_man

11:41 pm on Jun 18, 2007 (gmt 0)

10+ Year Member



Hi,

I have a little xmlhttprequest which opens a text file and puts it into a form.

The contents of the text file may contain special characters, such as an Æ which is Æ

When it goes into the form, it displays "?" instead of the special characters. I've tried changing my content type to charset=iso-8859-1 from utf-8 but that hasn't worked.

I've also tried this:


var s_p_c;
/*
Special characters array
*/
s_p_c = ['Æ', 'Á', 'Â', 'À', 'Å', 'Ã', 'Ä', 'Ç', 'É', 'Ê', 'È', 'Ë', 'Í', 'Î', 'Ì', 'Ï', 'Ñ', 'Ó', 'Ô', 'Ò', 'Ø', 'Õ', 'Ö', 'Ú', 'Û', 'Ù', 'Ü', 'Ý', 'á', 'â', 'æ', 'à', 'å', 'ã', 'ä', 'ç', 'é', 'ê', 'è', 'ë', 'í', 'î', 'ì', 'ï', 'ñ', 'ó', 'ô', 'ò', 'ø', 'õ', 'ö', 'ß', 'ú', 'û', 'ù', 'ü', 'ý', 'ÿ', 'Č', 'č', 'Ě', 'ě', 'Ň', 'ň', 'Ř', 'ř', 'Š', 'š', 'Ů', 'ů', 'Ž', 'ž', 'ő', 'Ő', 'ű', 'Ű', 'Ą', 'ą', 'Ć', 'ć', 'Ę', 'ę', 'Ł', 'ł', 'Ń', 'ń', 'Ś', 'ś', 'Ź', 'ź', 'Ż', 'ż', 'Ă', 'ă', 'Ğ', 'ğ', 'İ', 'ı', 'Ş', 'ş'];
var s_p_c_l;
s_p_c_l = ['Æ', 'Á', 'Â', 'À', 'Å', 'Ã', 'Ä', 'Ç', 'É', 'Ê', 'È', 'Ë', 'Í', 'Î', 'Ì', 'Ï', 'Ñ', 'Ó', 'Ô', 'Ò', 'Ø', 'Õ', 'Ö', 'Ú', 'Û', 'Ù', 'Ü', 'Ý', 'á', 'â', 'æ', 'à', 'å', 'ã', 'ä', 'ç', 'é', 'ê', 'è', 'ë', 'í', 'î', 'ì', 'ï', 'ñ', 'ó', 'ô', 'ò', 'ø', 'õ', 'ö', 'ß', 'ú', 'û', 'ù', 'ü', 'ý', 'ÿ', 'Č', 'č', 'Ě', 'ě', 'Ň', 'ň', 'Ř', 'ř', 'Š', 'š', 'Ů', 'ů', 'Ž', 'ž', 'ő', 'Ő', 'ű', 'Ű', 'Ą', 'ą', 'Ć', 'ć', 'Ę', 'ę', 'Ł', 'ł', 'Ń', 'ń', 'Ś', 'ś', 'Ź', 'ź', 'Ż', 'ż', 'Ă', 'ă', 'Ğ', 'ğ', 'İ', 'ı', 'Ş', 'ş'];

// s_o_n is the content
var i;
var r;
for (i=0;i<s_p_c_l.length;i++)
{
/*
Find all of a character
Replace all instances of it
Move to the next
*/
r=new RegExp(s_p_c_l[i],"g");
s_o_n=s_o_n.replace(r,s_p_c[i]);
}

But this doesn't work. Any ideas?

encyclo

1:35 am on Jun 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For Javascript, you need to use octal-encoded characters instead of HTML entity references. So for you example,
&AElig;
needs to be replaced with the octal-encoded equivalent which is
[b]\306[/b]
.

Here is a useful resource which maps ISO-8859-1 HTML entity references to their octal equivalent:

[pjb.com.au...]

sashaxiv

8:46 am on Jun 19, 2007 (gmt 0)

10+ Year Member



Another question about it. How can i do with de Database data with special characters? I come from spain and the "ñ" is used frecuently.

Regards, Kike

music_man

5:04 am on Jun 20, 2007 (gmt 0)

10+ Year Member



Thanks for the reference. I still don't know how to apply this...

My text file has the e.g. "e" acute, but not in octal or ascii, instead it is "é".

Is my loop for changing them correct?