Home:ALL Converter>how to make javascript functions together?

how to make javascript functions together?

Ask Time:2010-02-21T23:26:11         Author:Alex

Json Formatter

Below is the JavaScript code I use in my HTML page

<script type="text/javascript">
function loadXMLDoc(HTTP)
{
    var xmlHttp;
    try {  
        xmlHttp=new XMLHttpRequest();
    } catch (e) { 
        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=function() {
        if (xmlHttp.readyState==4) {
            alert(xmlHttp.responseText);
        }
    }  

    var params ="dd=123";
    xmlHttp.open("POST",HTTP,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params);
}
</script>

in the below javascript i wand to activate each ajax function

<script type="text/javascript">

// here i wand to send function together
return (loadXMLDoc('Page1.asp') && loadXMLDoc('Page2.asp') && loadXMLDoc('Page3.asp')); 
</script>

But here the problem is that I do not get the "return" (means 2nd & 3rd function not work)

Only the first function works

Hoping for your response

Author:Alex,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/2306334/how-to-make-javascript-functions-together
James Wiseman :

Remeber that in the statement:\n\nreturn (loadXMLDoc('Page1.asp') && loadXMLDoc('Page2.asp') && loadXMLDoc('Page3.asp')); \n\n\nlogically translates to:\n\nif (loadXMLDoc('Page1.asp')){\n if (loadXMLDoc('Page2.asp')){\n if (loadXMLDoc('Page3.asp')){\n return true;\n }\n }\n}\nreturn false;\n\n\nSo, each of the successive loadXMLDoc() function calls will only be called when if the previous function returns true.",
2010-02-21T15:51:36
Lachlan Roche :

Your function loadXMLDoc() does not return anything, add \"return true\" to the end of that function.\n\n xmlHttp.send(params);\n\n return true;\n}\n\n\nNote that a true return from loadXMLDoc() means you have successfully started an AJAX request. It will finish some time in the future, which will result in the onreadystatechange being called. Thus you are starting multiple AJAX requests in parallel.\n\nIf you wanted several AJAX calls in sequence, try something like the following:\n\nfunction doAjaxRequest( url, onreadystatechange )\n{\n var xmlHttp;\n try { \n xmlHttp=new XMLHttpRequest();\n } catch (e) {\n try { \n xmlHttp=new ActiveXObject(\"Msxml2.XMLHTTP\"); \n } catch (e) { \n try { \n xmlHttp=new ActiveXObject(\"Microsoft.XMLHTTP\"); \n } catch (e) { \n alert(\"Your browser does not support AJAX!\"); \n return false; \n } \n } \n }\n\n xmlHttp.onreadystatechange = onreadystatechange;\n\n var params = \"dd=123\";\n xmlHttp.open(\"POST\", url, true);\n xmlHttp.setRequestHeader(\"Content-type\", \"application/x-www-form-urlencoded\");\n xmlHttp.setRequestHeader(\"Content-length\", params.length);\n xmlHttp.setRequestHeader(\"Connection\", \"close\");\n xmlHttp.send(params);\n}\n\nfunction loadXMLDocs( HTTP )\n{\n var loadNextFile = function() {\n if (HTTP.length != 0) {\n var url = HTTP.unshift();\n doAjaxRequest( url, onreadystatechange );\n }\n }\n\n var onreadystatechange = function() {\n if (this.readyState==4) {\n alert(xmlHttp.responseText);\n\n loadNextFile();\n }\n }\n\n loadNextFile();\n}\n\nloadXMLDocs( ['Page1.asp', 'Page2.asp', 'Page3.asp'] );\n",
2010-02-21T15:39:31
yy