为什么图片加载不触发?

我想使用xmlhttprequest将文本和图像加载到div中,不幸的是,图像onload事件不起作用。

代码是这样的:

<img src="pic.png" onload="alert('...')"/>

请求后我可以看到图像,但是onload事件不会触发——为什么?

Can be down to caching. The onload event for images is a bit sketchy and doesn't always fire for AJAX requests.

Try adding a query parameter to the request with the current date to stop any caching

Instead of doing the javascript within the onload, just call a function, like this:

<img src="image.gif" onload="loadImage()" />

then in the head of your document, have this:

<script type="text/javascript">
    function loadImage()
{
alert("Image is loaded");
}
</script>

hope this helps!