[thelist] JavaScript variable scope wrong?

Kowalkowski, Lee (ASPIRE) lee.kowalkowski at hmrcaspire.com
Thu Aug 4 04:43:51 CDT 2005


Paul,

I'm not sure you will process the readystatechange event until you give the
browser a chance.  It's could be too busy executing your code.

You have (with significant line numbers):

	var req;
	var response;

	function loadXMLDoc(url)
	{
1		req = new XMLHttpRequest();
2		req.onreadystatechange = processReqChange;
3		req.open("GET", url, true);
4		req.send(null);
	}

	function processReqChange()
	{
5		// function body, may assign response.  [snipped]
	}

6	loadXMLDoc('urlToXML');
7	alert("1. " + response);

I think these lines will be executed in the following order: 6, 1, 2, 3, 4,
7, 5.  Basically between lines 6 & 7, your script has not "yielded" so no
events will be processed.  If the ready state changes prior to line 7 being
executed, the event will be held in a queue.

You could try the following to prove this:

7	setTimeout('alert("1. " + response)', 0); // (or perhaps 1).

-- LK


===========================================================
Our e-mail domain has now changed from iraspire.com to hmrcaspire.com. Please update your address books.
===========================================================



More information about the thelist mailing list