423,319 Members | 2,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,319 IT Pros & Developers. It's quick & easy.

How to determine when loading an xml file is complete? using javascript

P: n/a
hi,
My problem is how to determining when the XML file has loaded using
javascript.

I loaded an xml file using javascript in a web page.

The code below is loading the xml file for IE:
this.xmlDoc= new ActiveXObject("Microsoft.DomDocument");
this.xmlDoc.load(url);

The code below is loading the xml file for NS7 and FireFox0.9.3:
this.xmlDoc = document.implementation.createDocument("","doc",nu ll);
this.xmlDoc.load(url);

I want to make sure the xml file is loaded successfully, or to time
out at 0.5 seconds.

problems in IE:
I use a while loop to check this.xmlDoc.readyState after the load,
I didn't set the xmlDoc.asnyc to false since i want to time out if
loading is very time-consuming.

var today = new Date();
var now = today.getTime();
while(1)
{
//alert(this.xmlDoc.readyState);
var today2 = new Date();
var now2 = today2.getTime();
if((now2 - now) > 500 || (this.xmlDoc.readyState == 4))
{
alert(now2 - now);
return;
}
}
However, I get timed-out every time! Even I change 500 to 5000, IE
will hang for 5 seconds and time-out.There is only a small xml file to
load. It shouldn't take that long!

Is it because dom loads the xml asynchronously and the while loop
takes higher priority than the loading thread?Or is there any way i
can manipulate the multi-thread behavior?
If i use synchronous loading, how can I wait for a while in the func
before return and time-out the loading when necessary?

problems in NS, FireFox:
there is no property such as this.xmlDoc.readyState to check.

Is this.xmlDoc.onload a good way to detect the completion of loading?
What if i have to time out the loading after 0.5 seconds?

Any suggestion?

Thanks,
Roy
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ro********@gmail.com (Roy Wang) wrote:
hi,
My problem is how to determining when the XML file has loaded using
javascript.

I loaded an xml file using javascript in a web page.

The code below is loading the xml file for IE:
this.xmlDoc= new ActiveXObject("Microsoft.DomDocument");
this.xmlDoc.load(url);

The code below is loading the xml file for NS7 and FireFox0.9.3:
this.xmlDoc = document.implementation.createDocument("","doc",nu ll);
this.xmlDoc.load(url);

I want to make sure the xml file is loaded successfully, or to time
out at 0.5 seconds.

problems in IE:
I use a while loop to check this.xmlDoc.readyState after the load,
I didn't set the xmlDoc.asnyc to false since i want to time out if
loading is very time-consuming.

var today = new Date();
var now = today.getTime();
while(1)
{
//alert(this.xmlDoc.readyState);
var today2 = new Date();
var now2 = today2.getTime();
if((now2 - now) > 500 || (this.xmlDoc.readyState == 4))
{
alert(now2 - now);
return;
}
}
However, I get timed-out every time! Even I change 500 to 5000, IE
will hang for 5 seconds and time-out.There is only a small xml file to
load. It shouldn't take that long!

Is it because dom loads the xml asynchronously and the while loop
takes higher priority than the loading thread?Or is there any way i
can manipulate the multi-thread behavior?
If i use synchronous loading, how can I wait for a while in the func
before return and time-out the loading when necessary?

problems in NS, FireFox:
there is no property such as this.xmlDoc.readyState to check.

Is this.xmlDoc.onload a good way to detect the completion of loading?
What if i have to time out the loading after 0.5 seconds?

Any suggestion?


Don't use a busy loop to monitor the download. There's no such thing
as multi-threading in Javascript; if something is being processed then
nothing else is including the browser because it calls into the script
engine all the time to fire events. Implement your timeout using
setTimeout/clearTimeout.

xmldoc = new ActiveXObject("Msxml2.DOMDocument");
xmldoc.onreadystatechange = CheckState;
xmldoc.load(url);
timerId = setTimeout("Abort()", 500);

function CheckState()
{
var state = xmldoc.readyState;
if (state == 4)
{
clearTimeout(timerId);
// XML is loaded
}
}

function Abort()
{
if (xmldoc.readyState != 4)
xmldoc.abort();
}
Jul 23 '05 #2

P: n/a
Hi, Steve,
Thanks for your reply.
However, the reason i use a while loop is that i want to stay in the
function for 0.5 seconds unless the readyState is set to 4.
And I understand there is no such way like "sleep()" in javascript to do
that.

what if i use synchronous loading like this:

xmldoc = new ActiveXObject("Msxml2.DOMDocument");
xmldoc.onreadystatechange = CheckState;
xmldoc.async = false;
timerId = setTimeout("Abort()", 500);
xmldoc.load(url);

function CheckState()
{
var state = xmldoc.readyState;
if (state == 4)
{
clearTimeout(timerId);
// XML is loaded
}
}

function Abort()
{
if (xmldoc.readyState != 4)
xmldoc.abort();
}

will the function block at "load(url)" and time out after 500
miliseconds.
Or is there anyway to do that?

Thanks

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3

P: n/a
Roy Wang <no****@devdex.com> wrote:
Hi, Steve,
Thanks for your reply.
However, the reason i use a while loop is that i want to stay in the
function for 0.5 seconds unless the readyState is set to 4.
And I understand there is no such way like "sleep()" in javascript to do
that.

what if i use synchronous loading like this:

xmldoc = new ActiveXObject("Msxml2.DOMDocument");
xmldoc.onreadystatechange = CheckState;
xmldoc.async = false;
timerId = setTimeout("Abort()", 500);
xmldoc.load(url);

function CheckState()
{
var state = xmldoc.readyState;
if (state == 4)
{
clearTimeout(timerId);
// XML is loaded
}
}

function Abort()
{
if (xmldoc.readyState != 4)
xmldoc.abort();
}

will the function block at "load(url)" and time out after 500
miliseconds.
Or is there anyway to do that?


No and no, you have to do it like I outlined. Why do you want to stay
in the function?

Steve
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.