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.getEle mentById('page1 ');
var error = '';
var xmlhttp = false;
try{
xmlhttp = new ActiveXObject(" Msxml2.XMLHTTP" );
}catch(e){
try{
xmlhttp = new ActiveXObject(" Microsoft.XMLHT TP");
}catch(E){
xmlhttp = false;
}
}
if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
xmlhttp = new XMLHttpRequest( );
}
xmlhttp.open("H EAD",page1.valu e,true);
xmlhttp.onready statechange = function(){
if(xmlhttp.read yState == 4){
if(xmlhttp.stat us != 200){
error = "Invalid URL";
}
}
}
xmlhttp.send(nu ll);
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.getEle mentById('page1 ');
var error = '';
var xmlhttp = false;
try{
xmlhttp = new ActiveXObject(" Msxml2.XMLHTTP" );
}catch(e){
try{
xmlhttp = new ActiveXObject(" Microsoft.XMLHT TP");
}catch(E){
xmlhttp = false;
}
}
if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
xmlhttp = new XMLHttpRequest( );
}
xmlhttp.open("H EAD",page1.valu e,true);
xmlhttp.onready statechange = function(){
if(xmlhttp.read yState == 4){
if(xmlhttp.stat us != 200){
error = "Invalid URL";
}
}
}
xmlhttp.send(nu ll);
if(error && error != ''){
alert(error);
return false;
}else{
return true;
}
}
Comment