I’m having this code which seem to screw up itself when the text in the XMLHttpRequest contains Hebrew characters, so I’ve made a small example to demonstrate the problem.
The small code example loads the page and then replaces the content of a div with the content it got from the server.
Since it calls the same webpage then it should replace it with the same content.
The code works fine when the button inside the div contains letters in English, but if the letters are beyond ASCII 127 then it screws the button.
I’ve made some tests and saw that if I save the file as Unicode, then it works ok…
The problem is that I can’t save this file as Unicode since it:
- doubles the size of the file
- the hardware that writes some of the data in that html file, write it in ASCII
The small example just load itself:
<HTML><HEAD><TITLE>Test</TITLE>
</HEAD>
<BODY onload="readPage()">
<Div id='Hello'>
<Button>שלום</Button>
</Div>
<Script>
var xmlHttp;
function readPage()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="/xampp/2.html?dummy=" + new Date().getTime();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url, true);
xmlHttp.setRequestHeader("Content-Type", "text/plain;charset=ISO-8859-1");
xmlHttp.setRequestHeader("Accept-Charset", "ISO-8859-1");
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
var x = "";
//alert(xmlHttp.responseXML);
//alert(xmlHttp.responseText);
if (xmlHttp.responseText.match(/<Div id='Hello'>([\s\S]*?)<\/Div>/i))
{
x = xmlHttp.responseText.match(/<Div id='Hello'>([\s\S]*?)<\/Div>/i)[1];
//x = x.replace(/"/g,'');
var pdata= document.all ? document.all["Hello"] :
document.getElementById("Hello");
alert(pdata.innerHTML);
alert(x);
pdata.innerHTML=x;
alert(pdata.innerHTML);
}
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</Script>
</BODY></HTML>
I tried changing the char set and the content type, but it doesn’t work.
What am I doing wrong?
(btw, I cannot use any jQuery stuff).
Thanks.
Ok, I’m not sure what the web server of XAMPP sends for the specific HTML I use, but on the specific hardware, we wrote the web server and it sends:
One solution would be saving the web page as Unicode, which will not be good for us.
The other option, which seem to work, is to tell the browser that we want a specific encoding.
In this case Windows-1255.
All I had to do, is to change the header that is sent from the website so it will also contain the charset, and now it works fine: