跨浏览器图像onload事件处理

时间:2012-05-01 20:36:10

标签: jquery

在加载事件http://api.jquery.com/load-event/的jQuery文档中,它说 Can cease to fire for images that already live in the browser's cache。是否有更多关于此的信息,例如它影响的浏览器以及在什么情况下?

1 个答案:

答案 0 :(得分:10)

我不确定哪些浏览器受到影响,但很容易检查。

var img = new Image();
img.src = "foo.jpg";
if (img.complete || img.readyState === 4) {
    // image is cached
    doneCallback();
}
else {
    $(img).on('load',doneCallback);
}

<强>更新

如果更改代码,它将始终在所有浏览器中触发加载事件。

var img = new Image();
$(img).load(doneCallback);
img.src = "foo.jpg";