XMLHttpRequest too slow

O

otto

I have a form validation function that is supposed to use
XMLHttpRequest to validate a url (i.e. make sure it returns a 200), but
the problem is sometimes it takes about 5 seconds to return anything
and sometimes it returns right away with the wrong response.

What am I doing wrong? Or is it something other than the javascript
maybe? Thanks.

function validate(myform){
//Verify if URLs are valid.
var page1 = document.getElementById('page1');
var error = '';
var xmlhttp = false;
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(E){
xmlhttp = false;
}
}
if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("HEAD",page1.value,true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status != 200){
error = "Invalid URL";
}
}
}
xmlhttp.send(null);
if(error && error != ''){
alert(error);
return false;
}else{
return true;
}
}
 
L

Lasse Reichstein Nielsen

otto said:
I have a form validation function that is supposed to use
XMLHttpRequest to validate a url (i.e. make sure it returns a 200), but
the problem is sometimes it takes about 5 seconds to return anything
and sometimes it returns right away with the wrong response.
....
xmlhttp.open("HEAD",page1.value,true);

The third argument, true, means that the connection will be
asynchroneous. That means that calling "send" will return
immediately, and not wait for the communication to finish.
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status != 200){
error = "Invalid URL";
}
}
}
xmlhttp.send(null);
if(error && error != ''){

Since communication is asynchroneous, it's probably not done
talking to the server yet when this code is executed. In fact,
since Javascript is generally not multithreaded, even if it is
done, the readystatechange handler has not been executed yet.

I can't see where the 5 second delay comes from in this code.
It could just be the browser hanging for some reason.

(btw, "error && error != ''" is superfluous, since '' counts as false.
Just "error" will do).


You could do a synchroneous connection instaead (false as third
argument to "open"). The there won't be any readystatechange events,
but the call to "send" will only return when the answer is ready.
 
M

Martin Honnen

otto wrote:

xmlhttp.open("HEAD",page1.value,true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status != 200){
error = "Invalid URL";
}
}
}
xmlhttp.send(null);

if(error && error != ''){
alert(error);
return false;
}else{
return true;
}

It doesn't make sense to have code here after the send() call checking
error, you should call the send method and any code that has to happen
once the request has been made and the response has been sent should go
into the onreadystatechange handler.
 
O

otto

This is great. That's exactly what I needed to know. Thanks to you and
Martin for the assistance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top