JS检测是否加载img

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS检测是否加载img相关的知识,希望对你有一定的参考价值。

  1. /*
  2.  * Special event for image load events
  3.  * Needed because some browsers does not trigger the event on cached images.
  4.  
  5.  * MIT License
  6.  * Paul Irish | @paul_irish | www.paulirish.com
  7.  * Andree Hansson | @peolanha | www.andreehansson.se
  8.  * 2010.
  9.  *
  10.  * Usage:
  11.  * $(images).bind('load', function (e) {
  12.  * // Do stuff on load
  13.  * });
  14.  *
  15.  * Note that you can bind the 'error' event on data uri images, this will trigger when
  16.  * data uri images isn't supported.
  17.  *
  18.  * Tested in:
  19.  * FF 3+
  20.  * IE 6-8
  21.  * Chromium 5-6
  22.  * Opera 9-10
  23.  */
  24. (function ($) {
  25. $.event.special.load = {
  26. add: function (hollaback) {
  27. if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
  28. // Image is already complete, fire the hollaback (fixes browser issues were cached
  29. // images isn't triggering the load event)
  30. if ( this.complete || this.readyState === 4 ) {
  31. hollaback.handler.apply(this);
  32. }
  33.  
  34. // Check if data URI images is supported, fire 'error' event if not
  35. else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
  36. $(this).trigger('error');
  37. }
  38.  
  39. else {
  40. $(this).bind('load', hollaback.handler);
  41. }
  42. }
  43. }
  44. };
  45. }(jQuery));

以上是关于JS检测是否加载img的主要内容,如果未能解决你的问题,请参考以下文章

JS 图片滚动懒加载

测试白屏怎么排查

检测何时通过 ajax 加载资源

用PHP检测白色图像背景?

Android检测富文本中的<img标签并实现点击效果

使用 JS 检测网址能否正常加载