0

I'm having some difficulties making a "pre-load" script/css ecc... to load a background image before anything else.

I need this trick, because the background image is a gradient starting from white and the background color is a grey one.

Anybody can help me ? I tryed few methods ( css background, javascript onload ecc... )

CODE:

in head

<script type="text/javascript">
function preloadMyImages() 
{
var imageList = [
    "IMAGE.jpg"
];
for(var i = 0; i < imageList.length; i++ ) 
{
    var imageObject = new Image();
    imageObject.src = imageList[i];
}
}</script>

body

<body onload="preloadMyImages();">

css

#(id of element) { background:url("IMAGE.jpg") repeat-x top #c5c5c5;}
Lorenzo Pimps
  • 123
  • 1
  • 16
  • What kind of difficulties do you have specifically? Many articles about pre-fetching images exist. Post your code and explain what the problem is. – Felix Kling Aug 25 '14 at 15:08
  • Before DOM is hard, but with DOM: http://stackoverflow.com/questions/819336/how-to-preload-images-without-javascript. – loveNoHate Aug 25 '14 at 15:09
  • the code is simple so i don't need to paste this here. Imagine a background #999 and an image in gradient that start from #fff to #999. When the page loads i see the grey background, and after a while, the white gradient (when the background image is loaded of course). The problem is that the image is loaded sometimes after other images inside the webpage. The website as EXAMPLE is www.morlotti.com – Lorenzo Pimps Aug 25 '14 at 15:14
  • That doesn't explain how you are trying to preload the image. – Felix Kling Aug 25 '14 at 15:23
  • 1
    Code being simple doesnt matter, your simple code isn't working. Theres 20 different ways you could do this. We need to see your code to see what it is thats wrong. – Michael Aug 25 '14 at 15:24
  • added the code. This is what is NOT working at the moment – Lorenzo Pimps Aug 25 '14 at 15:32
  • It's not working because of where your code is in the page! When the parser sees your page it goes through it from top to bottom, meaning that if you have the code at the bottom of the page (as i suspect you do), it will not work. Move the script tag before the body. – Radu Andrei Aug 25 '14 at 15:39
  • ? the js is in the , the css in on head too... – Lorenzo Pimps Aug 25 '14 at 15:50

0 Answers0