[Wylug-help] AJAX, JS and scoping

Gary Stainburn gary.stainburn at ringways.co.uk
Fri Aug 17 11:27:04 BST 2007


Hi folks.

Having another go with AJAX and trying to create a library function based on a 
tutorial I've been looking at.

However, It's not working and I think it's a javascript scoping issue. In the 
call-back function it's complaining that xmlHttp doesn't exist when I try to 
check that readyState == 4.

AjaxRequest itself is obviously working as it should, but how to I make the 
xmlHttp created by it available to the call-back?

In my web page:

***
<SCRIPT LANGUAGE="JavaScript">
function getstaff(str) {
  if (str.length==0) { 
    document.getElementById("suggestlist").innerHTML="";
    return;
  }
  AjaxRequest("/roster/chooser_ajax.html?$actval:"+str,
     function() {
       if(xmlHttp.readyState==4) {         
document.getElementById("suggestlist").innerHTML=xmlHttp.responseText;
        }
      }
    );
}
</SCRIPT>
***

In my library

***
function AjaxRequest(Url,Fn) {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=Fn;
  xmlHttp.open("GET",Url,true);
  xmlHttp.send(null);
}
***
-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000     



More information about the Wylug-help mailing list