Site Navigation

Saturday, March 22, 2008

bug 279 - onload event for images might not happen

Issue: #279
Affects: IE6, IE7, Opera (ver?)

When you add an image to a page dynamically (via the DOM) and you set the .src attribute, if IE/Opera has it in the cache it is loaded instantly, thus if you set an event handler to catch the onload after setting the source, you're onload event may never fire.

Example:

<script type="text/javascript">
var myImg = document.getElementsByTagName('img')[0];
myImg.src = 'highlight.png';
myImg.onload = function(){
alert('image loaded!');
}
</script>



Known Workarounds: One. Move the event handler up to a position before the .src is declared.

Example Workaround Code:

<script type="text/javascript">
var myImg = document.getElementsByTagName('img')[0];
myImg.onload = function(){
alert('image loaded!');
}
//define src "after" the onload handler
myImg.src = 'highlight.png';
</script>



Related Issues: None.

Bug/Site Feedback |
Submit a bug