473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

javascript variable scope

Hi Experts, I'm new to JavaScript and have some problem to understand variable scope thing.

I declared two variables, xHTTP and xmlDoc as a global variable so I suppose it is accessible throughout the script. The following is my sample code

--------------------------------------------------------------------------------------------
var xHTTP;
xHTTP = new ActiveXObject("Msxml2.XMLHTTP");
var xmlDoc;
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
if (xHTTP != null)
{
xHTTP.onreadystatechange = gotTarget;
xHTTP.open('GET', 'test.aspx', true);
xHTTP.send( null );
}
function gotTarget()
{
if (xHTTP.readyState == 4)
{
if (xHTTP.status == 200)
{
xmlDoc.loadXML(xHTTP.responseXML.xml);
}
}
}
var i;
for (i=0;i<xmlDoc.getElementsByTagName("images")[0].childNodes.length;i++)
{
....
}
-----------------------------------------------------------------------------------------------------------------------

But on the line
for (i=0;i<xmlDoc.getElementsByTagName("images")[0].childNodes.length;i++)

it gives me error "Object required", which I believe it doesn't recognize xmlDoc object, so what's the problem? Why the object xHTML works, but not xmlDoc? Thanks for help!
Apr 26 '07 #1
3 1616
iam_clint
1,208 Expert 1GB
try alert(xmlDoc);

if it returns [OBJECT] then that means its possibly not finding the images.
Apr 27 '07 #2
it gives me the xml file.

I figured it might be the reason that the program executes the next Javascript line without waiting for the callback of XMLHTTP object, so xmlDoc is still empty when called upon. What I tried to do was, I intentionally put a whlie loop to delay the program until the object xmlDoc is filled

while (xmlDoc == null) {}

It worked, but problem is, when the page is loading, the browser popups a window telling the user that the script may cause the computer unresponsive. I know it's the empty while loop that is causing this problem. so my question is, is there a javascript time delay method that can delay the program execution without any Browser complaint?

Thanks again for your wonderful help!
Apr 27 '07 #3
pbmods
5,821 Expert 4TB
The best solution is to encapsulate your XMLHttpRequest call into an object, then execute the code (as a callback) when the request returns.

For more information, check out the prototype.js library, available at http://prototypejs.org/
Apr 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously undeclared variable. I can't beleive this is just for...
5
by: Jade | last post by:
I saw some web page saying I could do it this way in javascript: var iNumber = <%#publicvarname publicpropertyname %> but it doesn't seem to work. I have this piece of code here in...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
16
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
13
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
2
by: mr.mattyg | last post by:
I might as well start off like everyone else who posts problems they are having....So I'm new to JavaScript..... Anywho, I have a page that lists 15 or so thumbnails and then one big image of...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
60
by: marss | last post by:
Maybe anyone know good free online JavaScript knowledge test? This not exactly a system for testing online required - it may be simply list of questions with variants of answers (I have to prepare...
15
by: Sampat | last post by:
Hi, I wanted to know the performance of calling a function pointer v/s a normal function call in javascript in a scenario where there are multiple calls in the js to the same function. Please...
20
by: shapper | last post by:
Hello, How to create a namespace in Javascript containing two methods? And how to access those methods? Thanks, Miguel
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.