开发者

Javascript img onload

I have something like this:

<img class="side_image right" id="side_image" src="http://path/to/file_large.jpg" width="300" 
height="210" onload="document.getElementById('loading_gif').style.display = 'none';"  />

My problem is how to tell if the image does not exist. This block of code will hide the loading gif onl开发者_运维百科y if the image is loaded. But when there is really no image...I'm stuck with the loading gif. I still want to display something even if there is no image and set loading gif display = 'none';

Do you have any suggestions with just using native javascript/html or php. No jquery or other javascript libraries 'cause we're not allowed to use those tools.

Thanks...


onload = function() {
    var imageRef = document.getElementById('blah');
    imageRef.onerror = function(){
       this.src='blah.gif';
    }
}


To build off mender's answer...

<img src="image_to_load.jpg" onload="..." ... onerror="this.src='alternate_image.jpg';" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜