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

Javascript Image Object - Handle onload Event - Stack Overflow

programmeradmin3浏览0评论

I'm trying to preload image on click event:

// new image object
var imgObject = new Image();

// assign the path to the image to the src property
imgObject.src = document.URL + 'image/image.jpg';

// check if image has loaded
if (imgObjectplete) {

But the plete call never returns true on the first click - any idea what I'm missing here?

I'm trying to preload image on click event:

// new image object
var imgObject = new Image();

// assign the path to the image to the src property
imgObject.src = document.URL + 'image/image.jpg';

// check if image has loaded
if (imgObject.plete) {

But the plete call never returns true on the first click - any idea what I'm missing here?

Share Improve this question edited Sep 28, 2011 at 19:42 James Hill 61.8k22 gold badges149 silver badges166 bronze badges asked Sep 28, 2011 at 19:25 user398341user398341 6,58717 gold badges60 silver badges75 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

.plete is a property of the image object, not an event that you can attach to. Use the onload event:

var image = new Image();
image.onload = function() {
    alert('image loaded');
};
image.src = document.URL + 'image/image.jpg';

Note: Be sure to attach to the onload hander before setting the source attribute.

Note Explanation: Image caching. If the image is cached then the onload event will fire immediately (sometimes before setting the handler)

发布评论

评论列表(0)

  1. 暂无评论