What is the best way to set up a local fallback image if the external image does not load or takes too long to load.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can add an oneror handler:
<img src="http://example.com/somejpg.jpg"
onerror='this.onerror = null; this.src="./oops.gif"' />
Note setting onerror to null in the handler so that the browser doesn't die if oops.gif can't be loaded for some reason.
回答2:
Try to make use of the Image.complete property.
var img = new Image(w,h)
img.src = "http://...";
Now check periodically if img.complete
is true and call some fallback mechanism shuold it still be false
after n seconds.