通过Java API验证图像的URL

问题描述 投票:0回答:1

我从API获得了一系列图像URL,但是其中一些URL损坏了。有没有一种方法可以验证它们,然后在将它们附加到DOM之前检查它们是否没有损坏?

javascript vuejs2 vue-resource
1个回答
0
投票

???

function imgURL_checker (obj_URL_ChildNode_Arr)
    {
    var iterator=0, l=obj_URL_ChildNode_Arr.length;
        img=document.createElement ("img");

    function load ()
        {
        obj_URL_ChildNode_Arr[iterator].childNode.appendChild (img.cloneNode ());
        iterator++;
        next ();
        }

    function error ()
        {
        console.log ("URL not found: "+img.src);
        iterator++; 
        next ();
        }

    function next ()
        {
        if (iterator<l) img.src=obj_URL_ChildNode_Arr[iterator].url;
        else
            {
            console.log ("finished");

            //-- destroy
            img.removeEventListener ("load" ,load);
            img.removeEventListener ("error",error);
            img=null;
            }
        }

    img.addEventListener ("load",load);
    img.addEventListener ("error",error);
    next ();
    }

imgURL_checker ([{url:"https://cdn.pixabay.com/photo/2014/04/02/10/42/kitties-304268__340.png",childNode:document.body},{url:"http://___",childNode:document.body},{url:"https://cdn.pixabay.com/photo/2014/12/21/23/58/fox-576494__340.png",childNode:document.body}]);
© www.soinside.com 2019 - 2024. All rights reserved.