Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced <img> src change

Status
Not open for further replies.

mpopnoe

Programmer
Feb 28, 2002
47
0
0
US
I've found a lot of useful code but just can't seem to get this to work. Ok, the software shows images of real estate that a user searches for in a search results page. now, my databases image fields will always be populated since I auto-generate the photo filenames based on the MLS # of the property listing. I need to be able to check if the image exists (could be on any server so can't use server-side scripting), if it does then I need to display an image of a camera that represents that photos are available on the details page, if it is not a valid photo I don;t display anything. I've tried the code before and get 'stack overflow' errors and it is slow. I've also tried changing the src property directly in the onLoad and onError events of the <img> tag:

[here are the functions]
function isGood(id) {
//var cID=new String();
//alert('The ID is '+id);
document.getElementById(id).src=' alert('That image exists for '+id);
}

function isBad(id) {
document.getElementById(id).src=' alert('That image does not exist for '+id);
}

[here is the call (using FoxWeb)]
<img id="<<Current>>" src="<<aPhoto[Current]>>" onload="isGood(<<Current>>);" onError="isBad(<<Current>>);" height="25">

FoxWeb script is between the <<>>. The img tag is generated by a FoxPro program which iterates the counter 'Current'.

Any help is much appreciated!!
 
Why are you setting your image source in isGood() to the default page on the website?

Lee
 
Sorry for not being more clear - just ignore the URLs, in the code they point to actual photo files but they are MLS listings and I am not authorized to display them outside the designated site.
 
I have no idea the capabilities of FoxPro (I have never used it), but you can use your own server-side code to make http requests for the images. When you make the request, check the error code stats. If you do not get a 404, the image is there. If you do, the image is not there.

Jon

________________________________________
Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
Thanks Jon, I'll look into it - I just thought javascript would be the best route.

I almost got this working but I get a stack overflow error @ line 0 and it happens on the last image trying to be swaped and I can't figure out why - any ideas?

 
Your img tag has "onLoad" and "onError" withing the HTML tag. Those aren't supported as an HTML standard. A suggestion:

Let's say that FoxWeb is incrementing the <<Current>> variable based on the number of records returned from a result set.

Let's call the number of records, "totalRecords". In your javascript, put something like this:

Code:
var total = <<totalRecords>>;

Then, you could programmatically do some sort of "for" loop to do something to your images:
Code:
for (i = 0; i < total; i++) {
    document.getElementById(i).src = "url/to/image";
}

Again, however, your issue is in trying to determine if an image exists or not.

HTH,

Jon

________________________________________
Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
If they are supported why are they firing correctly? when the URL fails the onError fires and when it comes thourhg the onLoad fires. The problem at this stage is the 'stack overflow' errors - something to do with the image files but can't seem to pinpoint why.

thanks again for your insight!
 
Technically speaking, those tags are only supported in IE. They do not work in Mozilla/Opera/NS browsers. That's fine if you're in an IE-only environment, but if it's a tool for the world to use, you may want to re-think it's use.

I wish I could give you more help on this one, but the stack overflows...can't tell you where those are coming from. You could try the Javascript debugger in Mozilla, but the onError and onLoad calls you're making in the img tag won't fire.

Jon

________________________________________
Give a man a match, he'll be warm for a minute. But light him on fire, and he'll be warm for the rest of his life.
 
I'll take your advice, but I've been testing this site with Mozilla Firefox and IE 6 and onLoad and onError are firing away. Older browsers probably don't support it though so I'll look into doing it in FoxPro - I think I just need to use SOAP or some other component to make HTTP requests via a FoxPro program.

Thanks again for your tips and advice!

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top