admin管理员组文章数量:1186285
I'm trying to preload image and set the height and width to a container.
The problem seems to be with caching in ie8 since it fails to load on subsequent refreshes.
I've looked up and tried multiple solutions but seems nothing is working, at least not consistently.
current Javascript:
img = new Image();
img.src = '/images/site/image.jpg';
img.onload=function(){
var width = img.width + 'px';
var height = img.height + 'px';
$('#container').css({'width':width,
'height':height
});
};
Any suggestions are appreciated, thanks.
I'm trying to preload image and set the height and width to a container.
The problem seems to be with caching in ie8 since it fails to load on subsequent refreshes.
I've looked up and tried multiple solutions but seems nothing is working, at least not consistently.
current Javascript:
img = new Image();
img.src = '/images/site/image.jpg';
img.onload=function(){
var width = img.width + 'px';
var height = img.height + 'px';
$('#container').css({'width':width,
'height':height
});
};
Any suggestions are appreciated, thanks.
Share Improve this question edited Jan 23, 2013 at 10:05 Denys Séguret 382k90 gold badges809 silver badges775 bronze badges asked Jan 20, 2013 at 21:40 user1597002user15970022 Answers
Reset to default 30You must set the onload
callback before setting the src
.
When the image is cached, the onload callback isn't called with your code as the load
event is produced before the callback is set.
Do this :
img = new Image();
img.onload=function(){
var width = img.width + 'px';
var height = img.height + 'px';
$('#container').css({'width':width,
'height':height
});
};
img.src = '/images/site/image.jpg';
This is how retarded ie7 and ie8 are, even after setting the src AFTER the onload event, the event listener still misses it.
Check this out, this is what I finally had to do for one of my projects - ready for this? Settimeout. Yup! That's how retarded you have to write your code to get things to "work" in ie.
myImage.onload = doSomething;
var slowMeDown = setTimeout(function(){
myImage.src = "img/somePic.jpg";
},300);
Un-freaking-believable....
本文标签: javascriptonload callback on image not called in ie8Stack Overflow
版权声明:本文标题:javascript - onload callback on image not called in ie8 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738331019a2076039.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论