Javascript preloader for Content Management System

Advert test

I am trying to create a Javascript preloader for my Content Management System, especially for gallery pages, which currently have a bit of a delay, which doesn't look great.

However, most of the ones I have seen require the user to enter the filename of the image. I don't expect my users to do this on every page they update - and they don't have access to the source code anyway.

I have come up with this:

if (document.images) {
document.write("This document contains: " + document.images.length + " images.")
var x = document.images;
for (var i=0;i<x.length;i++)
var theSrc = x.src;
preload_image_object = new Image();

..but it only works when I put it at the bottom of the page, which defeats the purpose. The images still load with a delay.

Is there any way of putting Javascript at the top of the page, which will scan the rest of the code and preload any images it finds?