谁说不可以? 我这个版本可以:Firefox/2.0.0.16 二楼的,不是IE有时onload有时不,是你根本不了解它的机制,所以不要乱说好不?如果你懂得英文自己看看下面这段,我不费口舌了:Are you attaching an onload function to an image but finding it doesn't always get called?The problem is almost always this: you need to set the onload before you set the src. The reason is obvious when you think about it: if the image is in the cache, the image will be loaded immediately after setting the src, even before the onload is set.Adding fuel to the fire, Firefox and Safari will let you set an onload immediately after (it seems they don't call the onload until the block of JavaScript finishes). This doesn't happen in Internet Explorer or Opera.Long story short:// evil: var image = new Image(); image.src = 'image.jpg'; image.onload = function() { // sometimes called alert('image loaded'); };// good: var image = new Image(); image.onload = function() { // always called alert('image loaded'); }; image.src = 'image.jpg'; No matter how many times I come across this bug and find the solution, I end up making the same mistake a few weeks later. I'm hoping that blogging about it will get this stuck in my long-term memory.留个版权给人家: http://www.thefutureoftheweb.com/blog/image-onload-isnt-being-called
image.src = imagePath;
image.onload = function(){
alert('baaa');
alert(imagePath);
alert(image.fileSize);
}主要是ie下onload有时不执行,ff下完全不执行!
var image = new Image();
image.src = "http://www.jxxg.com/dhskin/logo.gif";
image.onload = function(){
alert('baaa');
alert(this.src);
alert(this.fileSize);
}
</script>
好像是路径问题,在调试中,ie和firefox的路径解释不同
我这个版本可以:Firefox/2.0.0.16
二楼的,不是IE有时onload有时不,是你根本不了解它的机制,所以不要乱说好不?如果你懂得英文自己看看下面这段,我不费口舌了:Are you attaching an onload function to an image but finding it doesn't always get called?The problem is almost always this: you need to set the onload before you set the src. The reason is obvious when you think about it: if the image is in the cache, the image will be loaded immediately after setting the src, even before the onload is set.Adding fuel to the fire, Firefox and Safari will let you set an onload immediately after (it seems they don't call the onload until the block of JavaScript finishes). This doesn't happen in Internet Explorer or Opera.Long story short:// evil:
var image = new Image();
image.src = 'image.jpg';
image.onload = function() {
// sometimes called
alert('image loaded');
};// good:
var image = new Image();
image.onload = function() {
// always called
alert('image loaded');
};
image.src = 'image.jpg';
No matter how many times I come across this bug and find the solution, I end up making the same mistake a few weeks later. I'm hoping that blogging about it will get this stuck in my long-term memory.留个版权给人家: http://www.thefutureoftheweb.com/blog/image-onload-isnt-being-called