The background to this is that I have two server-side scripts:
* upload.cgi: accepts a file upload
* monitor.cgi: reports the size of the file upload as the file upload progresses.
There is a form handler: onSubmit="doProgress();" for the upload form that points to upload.cgi. This does the asynchronous thing with monitor.cgi as the file uploads, and prints out the response from monitor.cgi. This is the javascript:
var req=null;
var url='/cgi-bin/monitor.cgi';
var HTTPMethod='GET';
function doProgress() {
window.setTimeout( sendRequest, 100 );
return true;
}
function sendRequest() {
if(req==null) { req=initialiseXMLHTTPRequest(); }
req.open(HTTPMethod,url,true);
req.setRequestHeader('content-type', 'text/plain');
req.onreadystatechange = listener;
req.send(null);
}
function initialiseXMLHTTPRequest() {
var xRequest=null;
try { xRequest = new XMLHttpRequest(); }
catch (e) {
try { xRequest = new ActiveXObject('MSXML2.XMLHTTP'); }
catch (e) {
try { xRequest = new ActiveXObject('Microsoft.XMLHTTP'); }
catch (e) {
// Failure!!
}
}
}
return xRequest;
}
var listener =function() {
if (req.readyState == 4 && req.status == 200) {
_toConsole(req.responseText);
}
}
function _toConsole(data) {
if(document.getElementById('console')) {
var console=document.getElementById('console');
var newline=document.createElement('div');
console.appendChild(newline);
var txt=document.createTextNode(data);
newline.appendChild(txt);
window.setTimeout( sendRequest, 1000 );
}
else { alert('console not defined'); }
}
This work perfectly with Firefox, IE8 and Opera. As the file upload progresses, we get the response from the monitor script.
But the listener function never works for Chrome or Safari. I can't understand why not! I've even tried accepting ANY readyState (not just "req.readyState == 4 && req.status == 200"), but still nothing gets passed to the _toConsole function.
I did read somewhere that there was a problem with onsubmit form handlers with Safari. But if I replace the sendRequest function with a simple "hello world" output, it works just fine in Safari & Chrome. (i.e. you see the line printed out every few seconds as the upload progresses). So I don't think it's that.