I've written some code that will check the natural size of an image at load time and if it fits a certain criteria then change the src. The script works fine but for some reason it doesn't work the first time I load the page.
If I refresh the page then the images are replaced as intended. I'm guessing it's something to do with the natural size not being available in the dom but can't seem to find any solution online.
Any help would be appreciated.
Thanks
Steve
Here's my code...
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('img').each(function(index){
// check if browser is html5 compatible or not
var img = jQuery(this);
if (typeof img.naturalWidth == "undefined") {
var newImg = new Image();
newImg.src = img.attr('src');
if(newImg.height == 80 && newImg.width == 80){
jQuery(this).attr("src", "/2/files/no_image.jpg")
}
}else{
if(img.naturalWidth == 80 && img.naturalHeight == 80)jQuery(this).attr("src", "/2/files/no_image.jpg");
}
});
});
</script>