473,408 Members | 2,734 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,408 software developers and data experts.

Image exists?

Hi. I've been trying to write Javascript code that checking if an image
exists. If it doesn't exist, I want to load a known image. I've tried:

if (document.images) {
var tester=new Image() // public variable
tester.onload="cImg('good')"
tester.onerror="cImg('bad')"
tester.src="http://www.temp.com/image.jpg"
}

function cImg(flg) {
if (flg == "good") {
document.images.main.alt = "Image is good"
document.images.main.src = "http://www.temp.com/image.jpg"
} else {
document.images.main.alt = "Known image"
document.images.main.src = "image.jpg"
}
}

Problem. As far as I can see, the onload and onerror don't fire.
Whether the image exists or not. At least in my IE6. What's the
problem? I've tried document.images.main.onload and onerror directly,
nothing. Any help is appreciated...Dennis
Jul 23 '05 #1
3 9660
Dennis Allen wrote on 19 mrt 2005 in comp.lang.javascript:
if (document.images) {
var tester=new Image() // public variable
tester.onload="cImg('good')"
tester.onerror="cImg('bad')"
tester.src="http://www.temp.com/image.jpg"
}

function cImg(flg) {
if (flg == "good") {
document.images.main.alt = "Image is good"
document.images.main.src = "http://www.temp.com/image.jpg"
} else {
document.images.main.alt = "Known image"
document.images.main.src = "image.jpg"
}
}

Problem. As far as I can see, the onload and onerror don't fire.
Whether the image exists or not. At least in my IE6. What's the
problem? I've tried document.images.main.onload and onerror directly,
nothing. Any help is appreciated...Dennis


Try this:

function good(){
alert('good')
}

function bad(){
alert('bad')
}

var tester=new Image() // public variable
tester.onload=good
tester.onerror=bad

tester.src="http://www.temp.com/image.jpg"

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
Ah. onclick, onerror, onload can't be a string. Must call the function
directly. Thanks...Dennis

"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...
Dennis Allen wrote on 19 mrt 2005 in comp.lang.javascript:
if (document.images) {
var tester=new Image() // public variable
tester.onload="cImg('good')"
tester.onerror="cImg('bad')"
tester.src="http://www.temp.com/image.jpg"
}

function cImg(flg) {
if (flg == "good") {
document.images.main.alt = "Image is good"
document.images.main.src = "http://www.temp.com/image.jpg"
} else {
document.images.main.alt = "Known image"
document.images.main.src = "image.jpg"
}
}

Problem. As far as I can see, the onload and onerror don't fire.
Whether the image exists or not. At least in my IE6. What's the
problem? I've tried document.images.main.onload and onerror
directly,
nothing. Any help is appreciated...Dennis


Try this:

function good(){
alert('good')
}

function bad(){
alert('bad')
}

var tester=new Image() // public variable
tester.onload=good
tester.onerror=bad

tester.src="http://www.temp.com/image.jpg"

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #3
Oops. Here's example:

var tester=new Image()
tester.onload=function() {cImg('good')}
tester.onerror=function() {cImg('bad')}
tester.src="http://www.temp.com/image.jpg"
Jul 23 '05 #4

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

Similar topics

8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
2
by: Mark Kamoski | last post by:
How can one change an image url at runtime? (Note, the image exists in a user control.) This is the image tag on the ascx... <asp:image id="TopBanner" runat="server"></asp:image> ....but...
3
by: anastasia | last post by:
I get an out of memory exception when attempting to excecute the following code: original = System.Drawing.Image.FromFile(file.FileName,true); I ONLY get this exception when the file is in the...
4
by: Simon Verona | last post by:
I'm using wia in a vb.net project to capture images from a scanner.. This works fine. However, I want to control the way that images are scanned - ie B+W, Greyscale and resolution etc, but can't...
2
by: Pete | last post by:
I'm sure the answer is simple, but the solution is eluding me-I'm not too good at the particulars of scripting. Maybe someone can point me on the right path... Here is the problem: I have a...
6
by: Rick Brandt | last post by:
I have a list of dynamically generated Parts that can be used to add to an Order. The list includes an image with the src set to "WhateverThePartNumberIs.png" in a particular folder. Not all...
3
by: Danny Ni | last post by:
Hi, I am looking for a way to display images with different aspect ratio into frames with fixed width and height, the problem is some images will look distorted if they are forced into fixed...
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.