Submit Your Article Forum Rules Search
WebProWorld
Register FAQ Calendar Mark Forums Read


Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Share Thread: & Tags

Share Thread:

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2004, 05:58 PM
WebProWorld Member
 
Join Date: Nov 2003
Location: UK
Posts: 35
iain RepRank 0
Default Using onerror to check existance of image

I need to be able to check in Javascript whether an image exists and have been trying to use the onerror event, but just can't get it to work. Does anyone have any suggestions?

The code I have been using is as follows. What happens is that both the onerror AND the onload events execute. I'm using IE6 and Navigator 6.



Code:
function testImage {
    var tester=new Image();
    tester.onLoad=isGGood();
    tester.onError=isBBad();
    tester.src="testimage.jpg";
}

function isGGood() {
    alert('That image exists!');
}

function isBBad() {
    alert('That image does not exist!');
	}
Reply With Quote
  #2 (permalink)  
Old 07-12-2004, 12:46 AM
WebProWorld Veteran
 
Join Date: Apr 2004
Posts: 447
HardCoded RepRank 0
Default

That syntax is incorrect. You can't assign event handlers in that way. You are also missing parentheses on your testImage function.

Try
Code:
function testImage() {
    var tester=new Image();
    tester.onload=isGGood;
    tester.onerror=isBBad;
    tester.src="testimage.jpg";
}
Reply With Quote
  #3 (permalink)  
Old 07-12-2004, 06:52 AM
WebProWorld Member
 
Join Date: Nov 2003
Location: UK
Posts: 35
iain RepRank 0
Default

Thanks. I had taken the code out of another example and didn't realize that the onerror needed to be all lowercase and the () not in the event handler call.
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:57 PM.



Search Engine Optimization by vBSEO 3.3.0