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;
}
}
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;
}
}