Joran Den Houting
Joran Den Houting

Reputation: 3163

Ajax success not being called

I try do to a check if the cross domain image exists or not. If it exsists it has to be added to the div with the class photos. Here's my code:

var num = 6;
for(var i = 1; i <= num; i++) {
    var ThisURL = 'http://example.com/test-0' + i + '.jpg';
    var ThisImage = '<img src="http://example.com/test-0' + i + '.jpg" />';
    CheckImage(ThisURL, ThisImage);
 }

 function CheckImage(a, b) {
    $.ajax({
        url:a,
        type:'HEAD',
        error: function(){
            // nothing
        },
        success: function(){
                $( "div.photos" ).append(b);
        }
    });
 }

If I do a console log within the success function it won't log anything in the console, so the function is not being called..

Any idea's?

Upvotes: 0

Views: 111

Answers (1)

A. Wolff
A. Wolff

Reputation: 74410

Why not using instead:

function CheckImage(a) {
    $("<img/>").one('load', function () {
        $("div.photos").append(this);
    }).attr('src',a);
}

Upvotes: 2

Related Questions