2

I want to display temporary image in an HTML page until original image is loaded. How can I do that?

<!-- IMG -->
<img src="original.jpg" temporary-img="temp.jpg"/> <!-- i have no idea how to do that ! -->
WillardSolutions
  • 2,316
  • 4
  • 28
  • 38
Robeala
  • 91
  • 1
  • 8

2 Answers2

5

If the original image is still loaded you can make the following in the HTML:

<img src="temp.jpg" id="img" data-original-img="original.jpg"/>

If the DOM is loaded, you can use the image, eg. Exchange as follows:

$(function() {
  $('#img').attr('src', $('#img').attr('data-original-img'));
});

Or with Plan JS: http://www.w3schools.com/jsref/event_onload.asp

Daniel
  • 126
  • 2
  • 5
3

You can add a background image using CSS.

img {
  background: url('temp_image.png') no-repeat;
}
the-petrolhead
  • 597
  • 1
  • 5
  • 16
  • 1
    @RajshekarReddy it's not stupid... css load the background with a temporary image and when the image is load she's hover the background... it can help OP – Alexis Dec 28 '16 at 13:59
  • This doesn't work in case the loaded image has a transparent background, in which case you will see the default image in the back :( – Felipe Jun 18 '17 at 13:42