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

前端 未结 15 1338
挽巷
挽巷 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

    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!

提交回复
热议问题