开发者

Javascript img onload

开发者 https://www.devze.com 2023-01-21 15:24 出处:网络
I have something like this: <img class=\"side_image right\" id=\"side_image\" src=\"http://path/to/file_large.jpg\" width=\"300\"

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

精彩评论

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