PDA

View Full Version : Stuck between a rock and a XMLHTTP


rlbailey
06-11-2007, 11:15 PM
Hi there,
I'm running off of a unix server and I need a javascript that is cross-browser to check if a file exists (image and html files). After experimentation, ActiveX just isn't playing nicely so I decided to go with XMLHTTP.
I need a synchronous XMLHTTP response that will work with IE, Mozilla. Problem there is that (i guess) Mozilla won't attach a .onreadystatechange event to a synchronous request, so my function always returns null (the anyonomous readystatechangefunction will never be called in Mozilla). The code works fine in IE though and I get the correct return values.
Any suggestions/help/advice?


HTTP.fileExists = function(url){
var request = HTTP.newRequest();
var exists = null;

//Register an event handler to revieve synchronous notification.
//This code says what to do with the response, and ti appears
//in a nested function here before we have even submitted the request.
request.onreadystatechange = function(){
//if the request is finished and was successful
if(request.readyState ==4){
if(request.status ==200 ){ exists = true; }
if( request.status == 404) { exists = false; /*this is file not found error*/ }
} else { /*alert("Request was not finished");*/ }
}

//Make a GET request for a given URL. The third arg of false specifies a synchronous request
request.open("HEAD", url, false);

//Now send the request. Since it is a GET request, we pass null for the body.
request.send(null);

return exists;
}

picch
06-11-2007, 11:26 PM
Is it possible for you to use something such as PHP?

rlbailey
06-12-2007, 12:12 AM
I don't think so Garret. I made a PHP test page because I don't know PHP, I don't think it's working.

http://www.cs.arizona.edu/people/rbailey/phptest.php

For the record, this is not my server (yikes), so I can't install it. But that was a good suggestion!

picch
06-12-2007, 01:38 AM
Yeah it doesn't look like php is installed. I just tried both my U account & ECE account, and both were a no go either.....http://u.arizona.edu/~picch/test.php & http://ece.arizona.edu/~picch/test.php

Maybe one of the web guys can throw out an idea. My javascript is extremely limited.

fischerm
06-12-2007, 09:50 AM
Is the file you are testing for living on the same server as this page with the javascript? Firefox has a security zone system that blocks access to remote pages vie javascript requests. Open up your error window in Firefox next time and I'll bet you see a "Permission denied on method ....".

emurphy1
06-12-2007, 09:57 AM
Another suggestion...try installing Firebug (https://addons.mozilla.org/en-US/firefox/addon/1843) in Firefox and use it to debug what is going on with your javascript.

rlbailey
06-14-2007, 03:42 PM
Aha! I figured it out. Since my XMLHTTP request is synchronous, it finishes the request after the request.send(null); line. Why do I need a statechange function then ? Well, I don't. I can figure out the state on the very next line. I think. I need to do some more testing on a browser where firefly is not installed.

Murphy, thanks for the tip on firebug. That link was invaluable. Now I just need to figure out how to use it to debug my javascript.

Woah. Firefly trips out my firefox. Somehow, after I installed firefly, all of a sudden Mozilla calls the request.onstatechange() function . I read around that Mozilla Firefox will not call the request.onstatechange() function if you're request is synchronous, so this is really wierd.



Here is the new code in case you are curious, and much simpler.

HTTP.fileExists = function(url){
var request = HTTP.newRequest();
var exists = false;

//Make a HEAD request for a given URL; 3rd arg means do a synch. request
request.open("HEAD", url, false);

//Now send the request. Since it is a GET request, we pass null for the body.
request.send(null);
//The request was sent, now check the status. A 200 status is what we want, meaning the file was found.
if( request.status ==200)
exists = true;

return exists;
}