3
var image = new Image();

image.onLoad = function() {
  alert("loaded");
}

image.onError = function() {
  alert("not loaded");
};

image.src ="https://s3.amazonaws.com/profileImages.mySample.com/spiderman.png" //dummy URL for reference only.

This is my code. I am fetching my images from amazon services, problem is that if I am getting some error like:

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>ABCDEFGHIJ</RequestId>
<HostId>
AABCDEFGHIJKLMNOPQRST
</HostId>
</Error>

then my code is not executing the image.onError().

how to check whether image exist at the URL ?

lavrton
  • 18,973
  • 4
  • 30
  • 63
robieee
  • 364
  • 2
  • 18
  • 1
    I think this [link][1] might help you. [1]: http://stackoverflow.com/questions/14651348/checking-if-image-does-exists-using-javascript – Nouphal.M Nov 29 '13 at 06:18
  • The S3 hosting may not be configured for public access ?? Can you simply download the image via its url from the browser ? – sanket Nov 29 '13 at 07:04
  • @sanket: yes able to get the image in browser. earlier making some other mistake. – robieee Nov 29 '13 at 07:14
  • 1
    I'm assuming you've realised that there is a big difference between `onerror` and `onError` ? The former will work, where-as the latter wont -- at least for modern browsers. – Pebbl Nov 29 '13 at 11:42
  • @pebbl: yes i got that... struggled for almost an hour to make it work, finally got it working after replacing just "E" with "e"... – robieee Dec 02 '13 at 06:38

1 Answers1

0

DEMO

var image = new Image();

image.onload = function() {
  alert("loaded");
}

image.onerror = function() {
  alert("not loaded");
};

image.src ="https://s3.amazonaws.com/profileImages.mySample.com/spiderman.png";
Voonic
  • 4,667
  • 3
  • 27
  • 58