Fallback image and timeout - External Content. Javascript

China☆狼群 提交于 2019-11-30 06:26:01

问题


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.



来源:https://stackoverflow.com/questions/1588854/fallback-image-and-timeout-external-content-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!