img readyState problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael J Whitmore

    #1

    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.hasChildN odes()) {" 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.oute rHTML" +fig.outerHTML) ;
    if (!fig.hasChildN odes()) {
    //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.widt h;
    Countimgcommfig = 0;
    if (ImageWidth == 28) {
    // Don't do anything since the file doesn't exist
    //alert("imgcommf ig doesn't exist, width of 28");
    } else {
    ReadyState = imgcommfig.read yState;
    while (ReadyState != "complete" && Countimgcommfig <
    100000) {
    Countimgcommfig = Countimgcommfig + 1;
    ReadyState = imgcommfig.read yState;
    ImageWidth = imgcommfig.widt h;
    //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.readyS tate;
    while (ReadyState != "complete" && Countimgprint < 100000)
    {
    Countimgprint = Countimgprint + 1;
    ReadyState = imgprint.readyS tate;
    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.wid th == 28) {
    if (imgprint.width == 28) {
    //alert("Printing img");
    fig.insertBefor e(img, fig.firstChild) ;
    } else {
    //alert("Printing imgprint");
    fig.insertBefor e(imgprint, fig.firstChild) ;
    imgprint.width = img.width
    imgprint.height = img.height
    }
    } else {
    //alert("Printing imgcommfig");
    fig.insertBefor e(imgcommfig, fig.firstChild) ;
    }
    } else {
    //alert("Yes Child Nodes");
    }
    }
  • Lasse Reichstein Nielsen

    #2
    Re: img readyState problem

    michaeljwhitmor e@netscape.net (Michael J Whitmore) writes:
    [color=blue]
    > 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.[/color]

    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?
    [color=blue]
    > The chosen image will then be inserted into the document as a child
    > element of ‘fig'.[/color]
    [color=blue]
    > 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.[/color]

    Odd. But that's what asynchroneous download will do to you.
    [color=blue]
    > The 100,000 value is simply a way to keep this out of an infinite
    > loop.[/color]

    Busy waiting is never a good choice.
    [color=blue]
    > The preferred order of images to chose from is imgcommfig, imgprint,
    > img[/color]

    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.hasChildNo des()) {
    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,st ate) {
    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;fini sh(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(prin t,10); // gives Mozilla time to update
    }

    var mkImgload =
    function(num,st ate){return function(){imgl oad(num,state); };};
    for (i=0;i<urls.len gth;i++) {
    imgs[i]=new Image();
    imgs[i].onload = mkImgload(i,tru e);
    imgs[i].onerror = mkImgload(i,fal se);
    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 - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    Working...