473,320 Members | 1,939 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.

img readyState problem

I am getting tired of losing hair over this. Here is a function that
simply inserts one of three images into a document right before
printing. It is called for every element that has a specific id in
the document with the onbeforeprint window call. The chosen image
will then be inserted into the document as a child element of ‘fig'.

The problem is the images decide on their own whether or not to show
up to the party. Random images constantly remain in a readyState of
uninitialized. However, for some reason when an alert box is shown
exactly 2 times the image will come out of the uninitialized state to
the complete state.

I don't want my clients to have to hit 2 alert boxes for each image
that is to be printed. How can I force these stupid images out of the
uninitialized state without 2n alert boxes?

Notes:
The element fig has no child nodes in the document. That is where the
image goes and why there is a "f (!fig.hasChildNodes()) {" test. Once
inserted they will stay in the document unless it is closed. No need
to reload something that is already there.
The 100,000 value is simply a way to keep this out of an infinite
loop.
The preferred order of images to chose from is imgcommfig, imgprint,
img

function showfig(fig) {
//alert("fig.outerHTML" +fig.outerHTML);
if (!fig.hasChildNodes()) {
//alert("No Child Nodes");
img = new Image();
imgprint = new Image();
imgcommfig = new Image();

img.src = "figures/" + fig.src;
imgprint.src = "figures/print/" + fig.src;
imgcommfig.src = fig.src;
//if the print figure doesn't exist reload display figure for
printing
//width of 28 is the IE graphic not found illustration, ie the
block with an X graphic
ImageWidth = imgcommfig.width;
Countimgcommfig = 0;
if (ImageWidth == 28) {
// Don't do anything since the file doesn't exist
//alert("imgcommfig doesn't exist, width of 28");
} else {
ReadyState = imgcommfig.readyState;
while (ReadyState != "complete" && Countimgcommfig <
100000) {
Countimgcommfig = Countimgcommfig + 1;
ReadyState = imgcommfig.readyState;
ImageWidth = imgcommfig.width;
//alert("The imgcommfig has not loaded yet. \nIt's
Next ReadyState = " +ReadyState);
}
}
ImageWidth = imgprint.width;
Countimgprint = 0;
if (ImageWidth == 28) {
// Don't do anything since the file doesn't exist
//alert("imgprint doesn't exist, width of 28");
} else {
ReadyState = imgprint.readyState;
while (ReadyState != "complete" && Countimgprint < 100000)
{
Countimgprint = Countimgprint + 1;
ReadyState = imgprint.readyState;
ImageWidth = imgprint.width;
//alert("The imgprint has not loaded yet. \nIt's Next
ReadyState = " +ReadyState);
}
}
ImageWidth = img.width;
Countimg = 0;
if (ImageWidth == 28) {
// Don't do anything since the file doesn't exist
//alert("img doesn't exist, width of 28");
} else {
ReadyState = img.readyState;
while (ReadyState != "complete" && Countimg < 100000) {
Countimg = Countimg + 1;
ReadyState = img.readyState;
ImageWidth = img.width;
//alert("The img has not loaded yet. \nIt's Next
ReadyState = " +ReadyState);
}
}

if (imgcommfig.width == 28) {
if (imgprint.width == 28) {
//alert("Printing img");
fig.insertBefore(img, fig.firstChild);
} else {
//alert("Printing imgprint");
fig.insertBefore(imgprint, fig.firstChild);
imgprint.width = img.width
imgprint.height = img.height
}
} else {
//alert("Printing imgcommfig");
fig.insertBefore(imgcommfig, fig.firstChild);
}
} else {
//alert("Yes Child Nodes");
}
}
Jul 20 '05 #1
1 7270
mi**************@netscape.net (Michael J Whitmore) writes:
I am getting tired of losing hair over this. Here is a function that
simply inserts one of three images into a document right before
printing. It is called for every element that has a specific id in
the document with the onbeforeprint window call.
My spider sense crawls here. ID's are supposed to be unique in a
document, so every element that has a specific id should be at most
one element. Am I misreading something?
The chosen image will then be inserted into the document as a child
element of ‘fig'. The problem is the images decide on their own whether or not to show
up to the party. Random images constantly remain in a readyState of
uninitialized. However, for some reason when an alert box is shown
exactly 2 times the image will come out of the uninitialized state to
the complete state.
Odd. But that's what asynchroneous download will do to you.
The 100,000 value is simply a way to keep this out of an infinite
loop.
Busy waiting is never a good choice.
The preferred order of images to chose from is imgcommfig, imgprint,
img


imgcommfig is the same as fig.src, so it should already be loaded?
I assume the order is increasing?
The readyState property is, AFAIK, IE only. You might want to use
the onload/onerror event handlers instead.

How about:
---
function showfig(fig) {
if (fig.hasChildNodes()) {
return true;
}

var urls = [ // order of decreasing preferrence
"figures/" + fig.src,
"figures/print/" + fig.src,
fig.src
];
var imgs = [];
var flags = [];
var done = false;
var imgload = function(num,state) {
if (done) {return;}
if (typeof (imgs[num].complete) == "boolean") {
state = imgs[num].complete; // Opera
}
flags[num]=state;
imgs[num].onerror = imgs[num].onload = function(){};
for (var i = 0; i < urls.length ;i++) {
if (flags[i] === true) {done=true;finish(i);return;}
if (flags[i] === undefined) {return;}
}
}
var finish = function(num) {
var img = imgs[num]; // best picture that loaded correctly
// do something:
fig.appendChild(img);
setTimeout(print,10); // gives Mozilla time to update
}

var mkImgload =
function(num,state){return function(){imgload(num,state);};};
for (i=0;i<urls.length;i++) {
imgs[i]=new Image();
imgs[i].onload = mkImgload(i,true);
imgs[i].onerror = mkImgload(i,false);
imgs[i].src = urls[i];
}
return false; // cancel printing until images have loaded!
}
---
(I can't seem to get the onerror handler to work in Opera 7.21, though.
I suspect a bug, so it will probably be fixed)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Richard Bell | last post by:
I would like to open a 'child' window win and check it's ready state. Unfortunately, when I try to do so, I discover that win.document does not appear accessable from the parent window, see below....
9
by: Eric Wallstedt | last post by:
I have a page that "logs" changes made to input fields using ajax to pass data to a cgi. I use POST and it works fine most of the time (all the time in IE). But it fails when I get the data from...
2
by: coolvirus | last post by:
I'm using the XMLHTTPRequest JavaScript object to fetch updated info from the server. The problem I'm having occurs on Windows 2000/IE 6. There is a very long pause on readyState 3 (close to a...
2
by: Mark Knochen | last post by:
Hi, i have a little problem with ajax. The follow functions are in my site: function sndReq(ID,divID) { resObjekt.open('get','inc/inc_change_pagelogo.php?ID='+ID,true);...
1
by: iporter | last post by:
I have several functions with code along the lines of: var xmlDoc = requestXML("ajax.asp?SP=SelectRelatedTags&tag=" + array); The requestXML() function includes the code: var xmlDoc = null;...
0
by: fadetoblack | last post by:
The code below is working to a point. At the http_request.readyState statement I get a state of one. How do I complete the process so I can get a state of four and/or another means of getting a value...
5
by: KingdomHeart | last post by:
I recently try to learn Ajax. But my code never got pass the xmlhttp.readyState because it is always = to 1. My callback function is OnData and it is call when onreadystatechange set. Line 40 is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.