0

i have two function in my project first:

function send(method,url,data,content_id,show_wait,small,aaa)
{
    if(content_id!='multipart'&&show_wait)
    {
        if(typeof(small) === 'undefined'){
            showwait(content_id);
        }else{
            smallshowwait(content_id);
        }
    }
    var XMLHttp=new XMLHttpRequest(); // is XMLHttpRequest httprequest class
    XMLHttp.onreadystatechange = function()
    {
        if(XMLHttp.readyState == 4 && XMLHttp.status == 200)
        {
            //alert(XMLHttp.responseText);
            if(content_id!='multipart')
            {
                MyResponse = XMLHttp.responseText.split('#$$#');
                if(typeof(aaa) === 'undefined'){
                    document.getElementById(content_id).style.display="none";
                    $(document.getElementById(content_id)).fadeIn("slow");
                }
            }
        }
    }
}

and second function is :

function aya(method,url,data,divid,isresponse){
    $(document).ready(function(){
        send(method,url,data,divid,isresponse,'small','aaa');
        $(document).ready(function(){
            alert('test');
            addActiveClass('inbox');
            send(method,'messages/showmessage.php',data,'messagecontent',true);
        });
    });
}

second function work correctly. but when i want remove alert('test'); error happen. in firebug error show :

document.getElementById(...) is null
there is way that i can remove alert?

aya
  • 1,597
  • 4
  • 29
  • 59
  • Why do you have two nested `ready` event handlers inside a function? There should be no need for any of them. – Felix Kling Feb 25 '13 at 10:55
  • i have'nt problem with `ready` i want remove `alert()` – aya Feb 25 '13 at 10:58
  • You can use $('#content_id') to get the id... – SKT Feb 25 '13 at 11:00
  • It's still unnecessary and confusing and you should fix it. Regarding the error: It's not apparent from the code you posted, but you might get it because you are trying to do something before the first Ajax request finished and the `alert` blocks the execution until it did. Change `send` to accept a callback and put all the code that you want to execute after the Ajax call inside that callback. More info [in this question](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call-from-a-function). – Felix Kling Feb 25 '13 at 11:01

1 Answers1

1

you can convert your code from :

alert('test');
addActiveClass('inbox');
send(method,'messages/showmessage.php',data,'messagecontent',true);

to :

setTimeout(function(){
        addActiveClass('inbox');
    },200);
    setTimeout(function(){
        send('post','messages/showmessage.php',data,'messagecontent',true);
    },1000);