CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   why readyState equal to zero (http://www.codingforums.com/showthread.php?t=250309)

objet 01-31-2012 08:38 PM

why readyState equal to zero
 
Hi,

the line 95
Code:

if (xhr.readyState == 4) {
equal to zero in function setWord();
1 - why ?
2 - did the browser run the code and when it come to line 95
it run but just one time if readyState equal to 4 the browser
continue successfully else (readyState equal to 0-3 ) then the browser will skip it and not wait to readyState parameter that will be equal to 4
Thanks :mad:;

the code:
Code:

        var str = "";
        var getString = "";
        var tempDiv = "";
        var newWord = "";
        var xhr = false;
        var url = "Favorite_Word.xml";
        var msg = "";
        var i,a;
       
       
       
function initAll() {
               
        str = document.getElementById("word").value;
        document.getElementById("word").className = "";
       
        if (str != ""){
                i = str.length;
                a = i;
                getstring = "";
                tempDiv = "";
                newWord = "";
                checkFullInputField();
        }
}

function checkFullInputField(){
        if_There_Request_XML();
        if (newWord!=""){
                while (i>0&&i<=mycars[0].length)
                          {
                            msg ="i:=" + i + "' a=i:=" + a;popmenu();
                            if (str[a-i].toLowerCase()== mycars[0][a-i].toLowerCase())
                              {msg ="a - i= " + (a-i) + " ," + "i:= " + i + " " + " ,First str="+ str[a-i];popmenu();
                                    getstring=getstring+str[a-i].toLowerCase();
                                    i--;}
                              else {msg ="you are in else i:= " + i;popmenu();i=-5;}

                          }
                        if (i==0){msg = "inputfield.getstring=" + getstring;popmenu();}
                            else {msg = "inputfield.getstring=" + getstring + "<br />"+"inputfield_str_1:" + str.substring(0,getstring.length);popmenu();}
        }
}



function popmenu(){
               
        document.getElementById("popups").innerHTML = "";
        var tempDiv = document.createElement("div");
                                tempDiv.innerHTML = msg;
                                tempDiv.className = "suggestions";
                                document.getElementById("popups").appendChild(tempDiv);
}

function if_There_Request_XML(){
        getHTTPObject();       
                alert("There is xhr :=2");
                if (xhr) {
                xhr.onreadystatechange = setWord();
                xhr.open("GET", url, true);
                xhr.send(null);
                alert("There is if xhr :=6");
        }
        else {
                alert("Sorry, but I couldn't create an XMLHttpRequest");
        }
}

function getHTTPObject()
{
 
        if (window.XMLHttpRequest) {
                xhr = new XMLHttpRequest();
                alert("There is getHTTPObject firefox:=1");
        }
        else {
                if (window.ActiveXObject) {
                        try {
                                xhr = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch (e) { }
                }
        }
}

function setWord() {
        var Node1 = "student";
        var Node2 = "name";

        if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                        if (xhr.responseXML) {

                                var wordData = xhr.responseXML.getElementsByTagName(Node1);
                                      alert("wordData:=" + wordData + ":=5 " );       
                        newWord = wordData[0].getElementsByTagName(Node2)[0].firstChild.nodeValue;
                                                alert("newWord:=" + newWord );
                                }
                        }
                }
                else {
                        alert("There was a problem with the request " + xhr.status + ":=3 4");
                }
        }


felgall 02-01-2012 01:11 AM

You have:

Code:

xhr.onreadystatechange = setWord();
which runs setWord are attaches whatever is returned to be run whenever the readyState changes. At this point it is 0 which is not equal to 4.

You don't return anything from the setWord function and so there is nothing to actually run when the state does change to 1, 2, 3 and eventually 4.

To get setWord to run when the state changes you need to change that line to read:

Code:

xhr.onreadystatechange = setWord;
That way the function will not run now and will instead run when the state changes.

objet 02-01-2012 01:04 PM

wow thanks alot
 
Thanks:thumbsup:


All times are GMT +1. The time now is 04:08 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.