Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 ;

    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");
    		}
    	}

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    wow thanks alot

    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •