473,383 Members | 1,874 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,383 software developers and data experts.

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

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
3 8465
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Donjuan | last post by:
Hi all I have trouble with tracking whether my image file is loaded. i use DHTML to change my image. HERE is the code: <img name="someimage" src="1.jpg"...
2
by: Sam Carleton | last post by:
I feel like a complete fool! I should know the answer to the Q: How do I load an image with JS and replace the default image? Some background: My final objective is to have a web site where it...
10
by: Roland | last post by:
Hello, the example code is the following(the number in parentheses at the beginning are just for reference)(The complete HTML file is at the end of this article): (1)window.location =...
4
by: zborisau | last post by:
Hey good people, I've been given a problem to solve recently - and stuck with the solution for a good 4 days already. i have a link which leads to popup window. the purpose of that popup...
8
by: Ray5531 | last post by:
Hi All, I got the following Error when I try to run my ASP.NET app ,hosted by IIS 5.1 in windows xp. Event Type: Error Event Source: ASP.NET 1.1.4322.0 Event Category: None Event ID: 1000...
6
by: magix | last post by:
Hi, when I read entries in file i.e text file, how can I determine the first line and the last line ? I know the first line of entry can be filtered using counter, but how about the last line...
1
by: agatha.life | last post by:
I did a javascript for the loading of images (I didn't want to have the images loaded in "on loading" because they are too many). The website is for a model and if you look at the codeof pages (...
13
by: mowsen | last post by:
Hello Group, i'm using a little "ajax" loader script to dynamically load files into different "div" tags on my main site. the code for this part looks like: function loader() { var args =...
2
by: barthelemy.von.haller | last post by:
Hi all, I googled and read this group but could not find any solution to my problem. I have a page to download big excel files that we 'build' on the server side. When a user click on the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.