views:

44

answers:

1

I am having a problem where my AJAX code does not get past the onreadtstate. The if( XMLHttpRequestObject) works fine, but the other part does not. The code is below:

enter code here function 
//Get the Ajax Object
getXmlHttpRequestObject() { 

 if (window.XMLHttpRequest  && !(window.ActiveXObject)) {
    XMLHttpRequestObject= new XMLHttpRequest();
    return XMLHttpRequestObject;
 }
 else if (window.ActiveXObject) {
     try{
     XMLHttpRequestObject=new ActiveXObject("Msxml2.XMLHTTP");
     return XMLHttpRequestObject;
     }catch(exception1){

         try{
            XMLHttpRequestObject= new ActiveXObject("Microsoft.XMLHTTP");
            return XMLHttpRequestObject;
         }catch(exception2){

         }//end exception 2
     }//end exception 1
 }//end if else
 else{
 document.getElementById('ajax_status').innerHTML='Status: Cound not create XmlHttpRequest Object.' +
'Consider upgrading your browser.';
 }
   }//end function getXmlHttpRequestObject() {

  function loadJavascript( src, url ){

XMLHttpRequestObject=getXmlHttpRequestObject();

if( XMLHttpRequestObject){
    //an alert will work here
    XMLHttpRequestObject.onreadystatechange = function()
    {
        alert("Here");
                    //Nothing at this pint works    
        if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
            includeJavaScript( sId, url, oXmlHttp.responseText );
        }
    }

}
}//end LoadJavaScript

Does anyone have an idea of what can be going wrong?

+3  A: 

You never send your request. The XMLHttpRequest doesn't do anything before you call open() and send() on it.

Also, don't forget to prefix your local variables with var when declaring them. Otherwise they'll be created as global and nasty things might happen.

Matti Virkkunen
@Matti is right. Also consider using a 3rd-party JavaScript library such as jQuery or Prototype. Writing browser-specific code is bygone, and these libraries will provide you with programming interface which is at best completely browser-independent.
themoondothshine