Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-31-2012, 08:38 PM   PM User | #1
objet
New to the CF scene

 
Join Date: Oct 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
objet is an unknown quantity at this point
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");
		}
	}
objet is offline   Reply With Quote
Old 02-01-2012, 01:11 AM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 02-01-2012, 01:04 PM   PM User | #3
objet
New to the CF scene

 
Join Date: Oct 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
objet is an unknown quantity at this point
wow thanks alot

Thanks
objet is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:32 AM.


Advertisement
Log in to turn off these ads.