javascript - How to detect if the image path is valid? -
i have found question regarding images
how detect if image path broken?
i have tried following codes
var image = new image; image.src = "http://project" + path + '.png'; image.onload = function(){ var imagewidth = this.width + this.height; if(imagewidth==0){ image.src = "http://project2" + path + '.png'; //the project2 path broken , //i want use project3 or project4 //path , keep testing it, there no way here. } }
would possible recursive
test here? lot!
you try setup:
var paths = ["/img1", "/img2", "/img3"]; var beginning = "http://project"; var ending = ".png"; function getimage(images, prefix, suffix, callback) { var iterator = function (i) { if (i < images.length) { var image = new image(); image.onload = function () { var imagewidth = this.width + this.height; if (imagewidth === 0) { console.log("onload problem"); iterator(++i); } else { console.log("onload good"); callback(i, image); } }; image.onerror = function () { console.log("onerror"); iterator(++i); }; image.src = prefix + images[i] + suffix; } }; iterator(0); } getimage(paths, beginning, ending, function (index, img) { console.log("callback: ", index, ", ", img); });
Comments
Post a Comment