
Signature
Martin Honnen
http://JavaScript.FAQTs.com/
Hey thanks for the fast reply,
I read about the UTF-8 encoding of the strings sent via responseText,
and i hoped i could setRequestHeader to the correct charset.. Doesn't
seem to work though..(unknown exception error)
I am trying the XML approach you suggested. Actually i am writing
manually an XML string and echoing it. All i get so far though is a
[object] in IE and nothing in firefox.
Apparently the responseXML returns an Document object. How can i print
it? I am not really interested right now in XML magic, but if wrapping
the data in XML helps i am willing to do that..
Any further hints..?
Thanks
Martin Honnen - 31 Dec 2004 14:02 GMT
> I read about the UTF-8 encoding of the strings sent via responseText,
> and i hoped i could setRequestHeader to the correct charset.. Doesn't
> seem to work though..(unknown exception error)
The PHP script needs to send UTF-8 encoded text so I am not sure what
you are trying to achieve with setRequestHeader on the client.

Signature
Martin Honnen
http://JavaScript.FAQTs.com/
Martin Honnen - 31 Dec 2004 14:49 GMT
> I am trying the XML approach you suggested. Actually i am writing
> manually an XML string and echoing it. All i get so far though is a
> [object] in IE and nothing in firefox.
I do not speak Greek so I am relying on babelfish for this. It
translates English
Who is god?
to the following:
Ποιος είναι Θεός;
Now assuming you are encoding your PHP pages as ISO-8859-7 which is an
8-bit encoding allowing you to use Latin letters and Greek letters you
could send your XML as ISO-8859-7 and then rely on the XML parser to
decode that. As said XML parsers do not need to understand that encoding
but my tests here with MSXML/IE 6, Mozilla and with Opera 8.00 beta show
they do understand that.
I have used the following PHP test page encoded as ISO-8859-7:
<?php
$text = 'Ποιος είναι Θεός;';
$xmlSource = '<?xml version="1.0" encoding="ISO-8859-7"?>' . "\r\n";
$xmlSource .= '<text xml:lang="gr">' . $text . '</text>';
header('Content-Type: application/xml');
echo $xmlSource;
?>
Then client-side script reads the XML sent as follows:
function getText (url, callback) {
var httpRequest;
if (typeof XMLHttpRequest != 'undefined') {
httpRequest = new XMLHttpRequest();
}
else if (typeof ActiveXObject != 'undefined') {
httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
if (httpRequest) {
httpRequest.open('GET', url, true);
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.responseXML) {
var xmlDocument = httpRequest.responseXML;
if (xmlDocument.documentElement &&
xmlDocument.documentElement.firstChild) {
callback(xmlDocument.documentElement.firstChild.nodeValue);
}
}
};
httpRequest.send(null);
}
}
function insertTextIntoBody (text) {
var p;
if (document.createElement && (p = document.createElement('p'))) {
p.appendChild(document.createTextNode(text));
document.body.appendChild(p);
}
}
getText('test2004123101.php', insertTextIntoBody);
and then the tested browsers show
Ποιος είναι Θεός;
in the HTML page.
Or use the PHP extension iconv if you have that available
http://de3.php.net/manual/en/function.iconv.php
to convert your 8-bit PHP strings to UTF-8 as needed.

Signature
Martin Honnen
http://JavaScript.FAQTs.com/
Vasilis Dimos - 31 Dec 2004 17:40 GMT
Hey Guys,
Thanks a lot for the answers. This is actually a lot more than I could
hope for. I will try this first thing in 2005. Happy new Year to all of
you.
Thanks Vasilis
Börni - 31 Dec 2004 15:23 GMT
> Hey thanks for the fast reply,
> I read about the UTF-8 encoding of the strings sent via responseText,
[quoted text clipped - 8 lines]
> Any further hints..?
> Thanks
I had a similar problem not long ago.
i past again some php code which should help you.
<?php
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n";
$xmlString .=
'<root><text xml:lang="de">Umlaute: ä, ö, ü</text></root>';
header('Content-Type: application/xml');
echo utf8_encode($xmlString);
?>
As you can see the xml string is build also manually, but is utf8
encoded before echoed.
In the Javascript again, you can access the responseXML object with the
normal W3C DOM Methods.
Have a look a this page for a good intro:
http://www.quirksmode.org/dom/intro.html