Check if an image is loaded (no errors) with jQuery

前端 未结 15 1340
挽巷
挽巷 2020-11-21 20:45

I\'m using JavaScript with the jQuery library to manipulate image thumbnails contained in a unordered list. When the image is loaded it does one thing, when an error occurs

相关标签:
15条回答
  • 2020-11-21 21:06

    This snippet of code helped me to fix browser caching problems:

    $("#my_image").on('load', function() {
    
        console.log("image loaded correctly"); 
    }).each(function() {
    
         if($(this).prop('complete')) $(this).load();
    });
    

    When the browser cache is disabled, only this code doesn't work:

    $("#my_image").on('load', function() {
         console.log("image loaded correctly"); 
    })
    

    to make it work you have to add:

    .each(function() {
         if($(this).prop('complete')) $(this).load();
    });
    
    0 讨论(0)
  • 2020-11-21 21:06

    I had a lot of problems with the complete load of a image and the EventListener.

    Whatever I tried, the results was not reliable.

    But then I found the solution. It is technically not a nice one, but now I never had a failed image load.

    What I did:

                        document.getElementById(currentImgID).addEventListener("load", loadListener1);
                        document.getElementById(currentImgID).addEventListener("load", loadListener2);
    
                    function loadListener1()
                        {
                        // Load again
                        }
    
                    function loadListener2()
                    {
                        var btn = document.getElementById("addForm_WithImage"); btn.disabled = false;
                        alert("Image loaded");
                    }
    

    Instead of loading the image one time, I just load it a second time direct after the first time and both run trough the eventhandler.

    All my headaches are gone!


    By the way: You guys from stackoverflow helped me already more then hundred times. For this a very big Thank you!

    0 讨论(0)
  • 2020-11-21 21:07

    As I understand the .complete property is non-standard. It may not be universal... I notice it seem to work differently in Firefox verses IE. I am loading a number of images in javascript then checking if complete. In Firefox, this seems to work great. In IE, it doesn't because the images appear to be loading on another thread. It works only if I put a delay between my assignment to image.src and when I check the image.complete property.

    Using image.onload and image.onerror isn't working for me, either, because I need to pass a parameter to know which image I am talking about when the function is called. Any way of doing that seems to fail because it actually seems to pass the same function, not different instances of the same function. So the value I pass into it to identify the image always ends up being the last value in the loop. I cannot think of any way around this problem.

    On Safari and Chrome, I am seeing the image.complete true and the naturalWidth set even when the error console shows a 404 for that image... and I intentionally removed that image to test this. But the above works well for Firefox and IE.

    0 讨论(0)
  • 2020-11-21 21:08

    Using this JavaScript code you can check image is successfully loaded or not.

    document.onready = function(e) {
            var imageobj = new Image();
            imageobj.src = document.getElementById('img-id').src;
            if(!imageobj.complete){ 
                alert(imageobj.src+"  -  Not Found");
            }
    }
    

    Try out this

    0 讨论(0)
  • 2020-11-21 21:10

    I tried many different ways and this way is the only one worked for me

    //check all images on the page
    $('img').each(function(){
        var img = new Image();
        img.onload = function() {
            console.log($(this).attr('src') + ' - done!');
        }
        img.src = $(this).attr('src');
    });
    

    You could also add a callback function triggered once all images are loaded in the DOM and ready. This applies for dynamically added images too. http://jsfiddle.net/kalmarsh80/nrAPk/

    0 讨论(0)
  • 2020-11-21 21:10

    This one worked fine for me :)

    $('.progress-image').each(function(){
        var img = new Image();
        img.onload = function() {
            let imgSrc = $(this).attr('src');
            $('.progress-image').each(function(){
                if($(this).attr('src') == imgSrc){
                    console.log($(this).parent().closest('.real-pack').parent().find('.shimmer').fadeOut())
                }
            })
        }
        img.src = $(this).attr('src');
    });
    
    0 讨论(0)
提交回复
热议问题