0

My Javascript code is like this :

var json = {
        "Hotel": [{
                "ImageUrl": "http://us.dotwconnect.com/poze_hotel/110802-img1-1.jpg"
            }, {
                "ImageUrl": "http://www.google.com/images/srpr/nav_logo14.png"
            }]
    };

    var totalHotel = json.Hotel.length;

    var image = '';
    for(var i=0;i<totalHotel;i++){

        hotelImage = json.Hotel[i].ImageUrl;



        // The "callback" argument is called with either true or false
        // depending on whether the image at "url" exists or not.
        function imageExists(url, callback) {
            var img = new Image();
            img.onload = function() { callback(true); };
            img.onerror = function() { callback(false); };
            img.src = url;
        }

        // Sample usage
        var imageUrl = hotelImage;
        imageExists(imageUrl, function(exists) {
            // console.log('RESULT: url=' + imageUrl + ', exists=' + exists);
             if(!exists){
                console.log('RESULT: url=' + imageUrl + ', exists=' + exists);
                hotelImage = 'https://az853139.vo.msecnd.net/static/images/not-found.png';
            }

        });

        image += '<img style="width:180px; height:120px; margin:9px;" src="'+hotelImage+'" />';


    }
    // console.log(image);
    $('#image').html(image);

Demo and complete code is like this : https://jsfiddle.net/oscar11/9omo18yn/

How to checking if image not found exists using javascript?

I try like that, but it's not working. Image "not found" does not appear

Any solution to solve my problem?

Thank you very much

moses toh
  • 12,344
  • 71
  • 243
  • 443

0 Answers0