The code preloads an image into an image object and then (supposed to) set it to the image element src on HTML:
<!DOCTYPE html> <html> <head> <script language="javascript"> window.onload = function () { var oImage = new Image(); oImage.onload = function () { document.getElementById('myImage').src = oImage; alert('done'); }; oImage.src = 'image1.jpg'; } </script> </head> <body> <img id="myImage" src="" /> </body> </html>
Why it doesn't work?