473,725 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.DomDo cument");
this.xmlDoc.loa d(url);

The code below is loading the xml file for NS7 and FireFox0.9.3:
this.xmlDoc = document.implem entation.create Document("","do c",null);
this.xmlDoc.loa d(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.rea dyState 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.xmlD oc.readyState);
var today2 = new Date();
var now2 = today2.getTime( );
if((now2 - now) > 500 || (this.xmlDoc.re adyState == 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.rea dyState to check.

Is this.xmlDoc.onl oad 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 8500
ro********@gmai l.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.DomDo cument");
this.xmlDoc.loa d(url);

The code below is loading the xml file for NS7 and FireFox0.9.3:
this.xmlDoc = document.implem entation.create Document("","do c",null);
this.xmlDoc.loa d(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.rea dyState 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.xmlD oc.readyState);
var today2 = new Date();
var now2 = today2.getTime( );
if((now2 - now) > 500 || (this.xmlDoc.re adyState == 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.rea dyState to check.

Is this.xmlDoc.onl oad 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.DOMDocum ent");
xmldoc.onreadys tatechange = CheckState;
xmldoc.load(url );
timerId = setTimeout("Abo rt()", 500);

function CheckState()
{
var state = xmldoc.readySta te;
if (state == 4)
{
clearTimeout(ti merId);
// XML is loaded
}
}

function Abort()
{
if (xmldoc.readySt ate != 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.DOMDocum ent");
xmldoc.onreadys tatechange = CheckState;
xmldoc.async = false;
timerId = setTimeout("Abo rt()", 500);
xmldoc.load(url );

function CheckState()
{
var state = xmldoc.readySta te;
if (state == 4)
{
clearTimeout(ti merId);
// XML is loaded
}
}

function Abort()
{
if (xmldoc.readySt ate != 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.DOMDocum ent");
xmldoc.onready statechange = CheckState;
xmldoc.async = false;
timerId = setTimeout("Abo rt()", 500);
xmldoc.load(ur l);

function CheckState()
{
var state = xmldoc.readySta te;
if (state == 4)
{
clearTimeout(t imerId);
// XML is loaded
}
}

function Abort()
{
if (xmldoc.readySt ate != 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
6196
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" onclick="document.all.someimage.src='someimage.jpg"> but how can i determine whether "someimage.jpg" is loaded? and can i get the download percent of a file? Thanks in advance
2
2745
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 is VERY difficult to copy images off the site. The first thing I was going to do was force folks to turn on JavaScript so that I could disable the right click pop up menu. To force this, I thought I would have all the default images say that...
10
16589
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 = 'http://www.google.com'; (2)alert("I'm still here!"); (3)window.open("http://www.slashdot.com", "_blank");
4
44086
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 window is to redirect user to the one of available servers. so i need to check which server is available. i have decided to download some 1pixel image from every server and if it was downloaded
8
2769
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 Date: 6/20/2005
6
5785
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 of entry in EOF while loop ? while (! file.eof() ) { ....
1
3406
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 ( for ex. http://www.agathamoon.com/gallery_48_eng.htm ) using explorer or firefox it works, with safari it doesn't! :( The idea is to have the word "loading" appearing while going from an image to another.
13
2531
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 = loader.arguments; switch (args) { case
2
9542
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 link to download, he has no clue that something is going on (and it can take some time). Indeed if the cursor is over some text or some special parts of the page it will not be set to the 'progress' type.
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.