homerun
homerun

Reputation: 20745

javascript cross domain image load -> check if loading image succeed loading

I have to load image from another site (cross domain)

my question is , i may can check somehow if the image has successfully loaded?

Upvotes: 1

Views: 5369

Answers (3)

Joseph Marikle
Joseph Marikle

Reputation: 78520

Typically you would put this in a load event listener like so:

var img = new Image();
img.onload = function(){
    alert(this.src + " loaded");
}
img.src="http://example.com/images/a.png";

Upvotes: 6

smfoote
smfoote

Reputation: 5609

If you're loading the image in an html tag, you can use the onerror event. For example:

<img src="https://otherdomain.com/img.jpg" onerror="handleImgError();" />

If handleImgError() runs, then you know that the image didn't load properly.

Upvotes: 0

Wasim Karani
Wasim Karani

Reputation: 8886

For Javascript Check this out

http://www.sajithmr.me/javascript-check-an-image-is-loaded-or-not

I know the way aroung with jQuery.

$('#image1')
.load(function(){
    $('#result1').text('Image is loaded!'); 
})
.error(function(){
    $('#result1').text('Image is not loaded!');
});

Upvotes: 0

Related Questions