最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if image is loaded? - Stack Overflow

programmeradmin1浏览0评论

Is it possible somehow to check is image loaded without function .load() or onLoad? Problem is that the webkit-like browsers doesn't work properly with this function.

UPDATE: Problem is in - load fires only once otherwise stream is going and loading new images.

Is it possible somehow to check is image loaded without function .load() or onLoad? Problem is that the webkit-like browsers doesn't work properly with this function.

UPDATE: Problem is in http://en.wikipedia/wiki/Motion_JPEG - load fires only once otherwise stream is going and loading new images.

Share Improve this question edited Sep 7, 2012 at 13:28 Andresh Podzimovsky asked Sep 7, 2012 at 13:05 Andresh PodzimovskyAndresh Podzimovsky 1,5614 gold badges13 silver badges17 bronze badges 5
  • 2 The .plete boolean property will tell you if the image is loaded. So if ( img.plete ) { ... }. (Note, though, that it probably isn't implemented in IEold.) – Šime Vidas Commented Sep 7, 2012 at 13:16
  • Webkit browsers support the "load" event for images. What do you mean? – Šime Vidas Commented Sep 7, 2012 at 13:21
  • They do not fire the event if the image is ing from cache. – vinczemarton Commented Sep 7, 2012 at 13:21
  • @SoonDead Yes. Browsers generally behave like that. – Šime Vidas Commented Sep 7, 2012 at 13:23
  • If I remember correctly IE-s don't. – vinczemarton Commented Sep 7, 2012 at 13:24
Add a ment  | 

2 Answers 2

Reset to default 4

You can check img.width : it's 0 if it's not loaded.

But I'm sure you can use onload (all low cap) on webkit browsers too :

 img.onload=function(){
     console.log('loaded!');
 };
 img.src='something';

Beware a frequent error : you have to set the src after you set the onload, or the onload callback won't be called if the image is in cache.

Demonstration : tested on Chrome, which is webkit based

I used this https://github./desandro/imagesloaded in a project and it worked fine for me

发布评论

评论列表(0)

  1. 暂无评论