Jump to content
johnnyg24

Help With Xmlhttprequest

Recommended Posts

I am trying to load a XML doc. with a function. However, I keep getting an error message in Firefox telling me that XDoc (my xml document) is undefined. I am loading this xml document the same way I do in other pages and it works fine. I don't know if my other functions are conflicting with it or not. Can anyone take a look and tell me if they see any problems. What I am trying to do is use cookies from other pages to locate information in the xml doc. The cookies that are brought over from the other page are the index numbers to the xml doc data that I am looking for. I'm not even sure if the doIt() function will work, but I can't even test it because I keep getting the error "xDoc is undefined". Any help would be much appreciated. Thanks.function to create the cookies:

function place(new_item){	//process old cookie	if (document.cookie && document.cookie != ""){var old_cookie = document.cookie;document.cookie = old_cookie + escape(new_item + "xxx");}//ends old cookie IFelse{document.cookie = "stuff=" + escape(new_item + "xxx");}//ends ELSE}//ends function

Script to extract the cookies, load the xml doc and locate the required information:

<script type="text/javascript">function loadXMLDoc(url) {		 var req = null;		 try {			req = new XMLHttpRequest();			req.overrideMimeType("text/xml");		 } catch(e) {			req = new ActiveXObject("Msxml2.XMLHTTP");		 }		 if (req) {		 	xDoc = null;			req.open("GET", "http://www.estout.com/xmlDocs/fabricSkus.xml", true);			req.onreadystatechange = function() {			   if (req.readyState == 4) {				  if (req.status == 200) {					 xDoc = req.responseXML;					 				  } else {					 alert("There was a problem retrieving the XML data:\n" + req.statusText);				  }				 			   }			}			req.send(null);		 }		 		  }if(document.cookie && document.cookie != ""){process_cookie()}//ends IFelse{alert("You haven't ordered anything or the cookies have been killed.")}//ends ELSEvar whole_fabricSkus_cookie = "";var fabricSkus_data = "";var fabricSkus_array = new Array();function process_cookie(){var whole_cookie = unescape(document.cookie);var each_cookie = whole_cookie.split(";");for (j=0; j < each_cookie.length; j++){if (each_cookie[j].indexOf("stuff") > -1){whole_fabricSkus_cookie = each_cookie[j]}}//ends sorting FORif (whole_fabricSkus_cookie != ""){fabricSkus_array = whole_fabricSkus_cookie.split("xxx");if (fabricSkus_array.length < 2){fabricSkus_array = "";}//ends nested IFdoIt();}//ends IF}//ends process cookie() function init() {		 loadXMLDoc("http://www.estout.com/xmlDocs/fabricSkus.xml");	  }	  	   	   	  function doIt(){	   if (fabricSkus_array != ""){for (j=1; j < fabricSkus_array.length; j++){		   for (var i = 0; i < xDoc.getElementsByTagName("sku").length; i++) {document.getElementById("textHere").innerHTML = xDoc.getElementsByTagName("desc")[fabricSkus_array[j]].firstChild.data;//document.write("<h3>" + fabricSkus_array[j] + "</h3>");}//ends diplay FOR}//ends fabricSkus display}}//window.onload = init;</script></head><body onload="init()"><xml id="reports"> </xml><div id="textHere"></div><br><br></body></html>

Share this post


Link to post
Share on other sites

xDoc appears to be local to that if statement inside loadXMLDoc(), so when you're using it later inside doIt() it doesn't exist.

Share this post


Link to post
Share on other sites

I tried making xDoc a global variable and it still doesn't work. In other pages where I am loading the same xml document using the same loadXMLdoc() function, it works just fine. It seems like something in the script it not allowing it to load properly.

Share this post


Link to post
Share on other sites

If you are using xDoc on the other pages that are using loadXMLDoc() then it is indeed strange that they work since the xDoc variable is definitely not visible outside that function unless you've defined it outside. The other problem I've just seen is that you call process_cookie() which calls doIt() directly inside the head BEFORE you even call loadXMLDoc(). But even if you had called loadXMLDoc() before, since it is asynchronous you need to make sure it has returned before calling doIt(). So you should be calling doIt() either directly or indirectly from the onreadystatechange function.

Share this post


Link to post
Share on other sites

EDIT: I got here before Pollux's reply above.I assume the error comes during the DoIt() function. That function gets called by process_cookie(). process_cookie() gets called in the global space of your script (not inside a function). That means it gets called while the script loads. loadXMLDoc() gets called after the page loads. So when DoIt() trys to access xDoc, xDoc does not yet exist -- and even it it had been declared in the global space (which is common) it is not yet an HTTP object until after the page loads. That is why xDoc is undefined.Your sequencing is off, that's all.Pollux, sorry, is wrong on both counts. The way the script is set up, xDoc must be a global variable. It can be declared with the var keyword only if it is declared in the global space (a good idea) but NOT inside a function. Using the var keyword there would make it local, and DoIt() will never see it. Also, current versions of Javascript do not localize the scope of variables to internal structures like if statements. A global can be declared anywhere and still be global. Future versions of Javascript I believe will have such a localizing ability (unless I'm remembering a different language I read about :) ), but even then I think such variables will need to be prefixed with a keyword.

Share this post


Link to post
Share on other sites

You're quite right, local variables are only local to the extent of the function they're in. I'm too much of a Java programmer :)

Share this post


Link to post
Share on other sites

Thank you, I was able to get it work, but now it seems as though the doIt() function gets, for my lack of better words, "stuck". I know this is not a good description, but the function does what it needs to, but the mouse button has a hourglass beside it and never goes away, so it looks like the function is either trying to repeat itself and never knows when to stop or it's getting freezing up. Here is the new code, any suggestions as to why I see that hourglass and how can I get it to work properly?

<script type="text/javascript">var xDoc = null;function loadXMLDoc(url) {		 var req = null;		 try {			req = new XMLHttpRequest();			req.overrideMimeType("text/xml");		 } catch(e) {			req = new ActiveXObject("Msxml2.XMLHTTP");		 }		 if (req) {		 	xDoc = null;			req.open("GET", "http://www.estout.com/xmlDocs/fabricSkus.xml", true);			req.onreadystatechange = function() {			   if (req.readyState == 4) {				  if (req.status == 200) {					 xDoc = req.responseXML;					 doIt();				  } else {					 alert("There was a problem retrieving the XML data:\n" + req.statusText);				  }					 			 			   }							}			req.send(null);		 }		 		  }var item_number = new Array(); if (document.cookie && document.cookie != ""){process_cookie()} function process_cookie(){var whole_cookie = unescape(document.cookie);var drop_name = whole_cookie.split("=");if (drop_name[1] != ""){item_number = drop_name[1].split("xxx");}//ends IF}//ends process_cookie() function function kill_cookies(){var kill_date = new Date("January 1, 1970");document.cookie = "stuff=stub;expires=" + kill_date.toGMTString();}//ends kill_cookie() function function remove(){var new_cookie_raw_data = "drop this"; if (document.orderform.orderboxes){for (i = 0; i < document.orderform.orderboxes.length; i++){if (document.orderform.orderboxes[i].checked == true){new_cookie_raw_data = new_cookie_raw_data + document.orderform.orderboxes[i].value + "xxx";}//ends IF}//ends FOR}//ends testing IF var drop_initialization = new_cookie_raw_data.split("drop this"); if (drop_initialization[1] == ""){kill_cookies()}//ends IF if (drop_initialization[1] != ""){var clean_data = drop_initialization[1];document.cookie = "stuff=" + escape(clean_data);}//end IF window.location = "samp177.htm";}//ends remove() function function init() {		 loadXMLDoc("http://www.estout.com/xmlDocs/fabricSkus.xml");	  } window.onload = init;</script>  </head>  <body> <h4>You've ordered the items below. Remove items by unchecking the appropirate box(es) and clicking the "Remove" button.</h4>  <form name="orderform"> <script language="JavaScript">function doIt(){ if (item_number.length < 1){document.write("<h4>You don't have anything ordered. Add items using the links below.</h4>");}//ends IF else{for (i = 0; i < item_number.length - 1; i++){document.write("<input type=\"checkbox\" name=\"orderboxes\" checked ");document.write(xDoc.getElementsByTagName("desc")[item_number[i]].firstChild.data); document.write(xDoc.getElementsByTagName("wPrice")[item_number[i]].firstChild.data); }//ends FOR}//ends main display ELSE}//ends function </script><br><br> <input type="button" value="Remove" onClick="remove()"> </form> <br><br>

Share this post


Link to post
Share on other sites

document.write() will ADD content to a page if it is executed while the page is loading. After the page has loaded (which it has here), document.write OBLITERATES the current document and starts a new one. It looks like you're trying to add elements to a form. That will not happen with this set up. You may end up with the elements, but they will exist with no document surrounding them.The hourglass is just a symptom of all this, possibly because you never call document.close(). But that is not important.What you want to do instead of using document.write() is add elements to the form element, either with DOM methods (appendChild() and so on) or through the innerHTML property. This will keep your document intact AND add the correct elements as you want them. Start by giving your form an id, then get a reference to the form with document.getElementById().

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...