Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Images after they are loaded completely

Tags:

html

jquery

php

I want to load the image after the image is completely loaded or downloaded in the browser. I dont want to display the image while it is being downloaded. Rather I would like to display a lower quality version of that image until the high quality images is loaded in the background, and when the image is completly loaded in the background, I want to replace the lower quality image with that image.

I have two images, say

$LQimage = "LQabc.jpg";

$HQimg = "HQabc.jpg";

How can I make it work?

EDIT With the answer posted by @codebox, this code worked.. thanks :)

<html>
<head>
<script type="text/javascript">
function load(){
    var img = new Image();
    var imgUrl = 'HQabc.jpg';
    img.onload = function(){
        // this gets run once the image has finished downloading
        document.getElementById('pp').src = imgUrl;
    }
    img.src = imgUrl; // this causes the image to get downloaded in the background
}
</script>
</head>
<body onload="load()">
    <img id="pp" src="LQabc.jpg" width=600/>
</body>
</html>
like image 511
prakashchhetri Avatar asked Sep 06 '12 08:09

prakashchhetri


1 Answers

This should do it:

function preLoadImage(){
    var img = new Image();
    var imgUrl = 'HQabc.jpg';
    img.onload = function(){
        // this gets run once the image has finished downloading
        document.getElementById('idOfImgElement').src = imgUrl;
    }
    img.src = imgUrl; // this causes the image to get downloaded in the background
}

If you run this function once the page has loaded it should work:

<body onload="preLoadImage()">
like image 123
codebox Avatar answered Sep 30 '22 16:09

codebox