Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5931749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:44:18+00:00 2026-05-22T14:44:18+00:00

I know my problem is the fact that I can’t check if the image

  • 0

I know my problem is the fact that I can’t check if the image is good until it has time to load. I’m trying to check width and return after it or the src changes (using onerror to set an src.) I’m always getting stuck with a race condition though, and it errors out long before the height and width or src change. When I reload, the image is cached and it works fine. I don’t know how to avoid this. Here is my current code (also not working, it loops until Firefox complains.) I’m setting the return of this function to a variable. There are some writes in there I’m using to see how far it gets, and it stops in the while loop. I tried using getTimeout('tstit = chkload("thisisatest",tinypth);',250);, but that didn’t work either. I wish I could force this to load in order…

function buildimg(tehname,myc,pth1,pth2,tinypth)
{
var myimg = pth1+tehname+"/"+tehname+"-"+myc+".jpg";
buildtest("thisisatest",myimg,tinypth);
var testimg = document.getElementById("thisisatest");
var tstit = chkload("thisisatest",tinypth);
while(tstit == false) {
      document.write(tstit);
      tstit = chkload("thisisatest",tinypth);
    }
alert(testimg.src+'-'+testimg.width+'-'+testimg.height);
if((testimage.width > 20) && (testimage.height > 20)) {
       return myimg;
    }
else if(typeof pth2 == "undefined") {
        myimg = "error";
        return myimg;
    }
else {
        myimg = buildimg(tehname,myc,pth2);
        return myimg;
    }
    
document.write('No return error in buildimg.');
return "error";
}

/*Builds a hidden img tag for testing images*/
function buildtest(itsid,itssrc,tinypath) {

 if(document.getElementById(itsid)) {
    var imgobj = document.getElementById(itsid);
    imgobj.remove();
    }
document.write('<img id="'+itsid+'" style="display: none;" src="'+itssrc+'" onerror="swaponerr(\''+itsid+'\',\''+tinypath+'\')"  />');
}

/*Swaps the image to a small picture, so we can detect if it worked*/
function swaponerr(tagid, tinypath) {
    var theimg = document.getElementById(tagid);
    theimg.onerror = '';
    theimg.src = tinypath;
}

/*Recurses to return when the image is loaded*/
function chkload(loadid,tinychk) {
var tehobj = document.getElementById(loadid);
document.write(tehobj.width+'x'+tehobj.height+'x'+tehobj.src);
if((tehobj.naturalWidth > 20) && (tehobj.naturalHeight > 20)) {
    return true;
    }
if(tehobj.src == tinychk) {
    return true;
    }
return false;
}

I need to test for an image, and return error if it is non-existent. The code below works fine on my server:

/*Checks if the image at /tehname/tehname-c.jpg exists in either of the paths
outputs it's address if it does, and "error" if not.*/
function buildimg(tehname,index,pth1,pth2)
{
var myimg = pth1+tehname+"/"+tehname+"-"+index+".jpg";
$.ajax({
url:myimg,
type:'HEAD',
error: function()
{
    myimg=pth2+tehname+"/"+tehname+"-"+index+".jpg";     
    $.ajax({
        url:myimg,
        type:'HEAD',
        error: function()
            {
            myimg="error";
            return;
        },
    });     
    return;
},
});
return myimg;
}

Unfortunately, I’m trying to do this on a messed up system my work uses. We do have jquery, but the system stores user files on a separate server from code, so ajax won’t work. This will eventually be in a .js file, I hope.

Now I’ve got code starting with:

function buildimg(tehname,myc,pth1,pth2)
{
  var myimg = pth1+tehname+"/"+tehname+"-"+myc+".jpg";
  var tehimage = new Image();
  tehimg.src = myimg;

I tried to have the function load the image, and check its width, but I always get 0, since I can’t pre-load the images without knowing how many they are (and I don’t want to have some outrageously high number of requests with most being errors.) For some reason (at least on Firefox 4, as that’s what my goal is to get working first) tehimage.complete always returns false. I’ve tried using onerror and onload by a global variable, and a few other methods. I must admit though, I’m not very versed in Javascript, so my callbacks may not have worked.

Please help, I’m getting desperate!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:44:19+00:00Added an answer on May 22, 2026 at 2:44 pm

    Since I’m getting no answers, I’m closing the question. What I ended up doing was a modification to make the image change class in the onload, have the image onerror delete it, and have the rest of the code run in the window onload. Unfortunately, this means the 404s aren’t caught before it tries to load more images, so I’m forced to limit the max number of images that can be used (changeable in the function call,) and the images that aren’t there just waste a little time.

    See the final result here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I've solved this problem before, but I can't remember or find the
So I have a problem that I can not figure out. I am writing
I am facing a problem that I can not understand . During a plugin
I have a problem that I have been thinking about, but I can't really
I've got an interface inheritance issue that has been vexing me for some time.
Assume that the array has integers between 1 and 1,000,000. I know some popular
Most of you probably know the following problem: You edit an HTML, view the
I don't know the exact problem. I am using flex mx:tree component in flex
I realy dont know what the problem is with VS2010. I created a class,
How do I let Microsoft know about a problem I've found in one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.