473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

good ole image preloader...

okay, the scenario:

i have a header image which changes randomly across all pages in the
site. that works fine. i also have an image preloader within the random
header picker which theoretically loads the header image before the
rest of the graphical content on the page. however, what appears to be
happening, judging by the status bar messages, is that the banner
always loads last.

here's the random header code -

function random_header()
{
no=Math.random()*14;
no=Math.round(no);
no = no + 1;
bname = "banners/banner_" + no + ".jpg";
preload=new Image();
preload.src=bname;
document.getElementById('rndimg').src=bname;
}

banners are all named sequentially, 'rndimg' is the blank header image
having its source changed.

any help is appreciated. thanks meantime :)

Mar 21 '06 #1
2 2033
bedges wrote:
okay, the scenario:

i have a header image which changes randomly across all pages in the
site. that works fine. i also have an image preloader within the random
header picker which theoretically loads the header image before the
rest of the graphical content on the page. however, what appears to be
happening, judging by the status bar messages, is that the banner
always loads last.
Why not randomise the banner on the server? Why rely on unreliable client
scripting when there seems to be zero benefit?

here's the random header code -

function random_header()
{
no=Math.random()*14;
no=Math.round(no);
no = no + 1;
bname = "banners/banner_" + no + ".jpg";
Keep variables local if they don't need to be global. If your image names
are in the range 1 to 15 inclusive:

var bname = 'banners/banner_'
+ (Math.floor(Math.random()*15) + 1)
+ '.jpg';

Or using bitwise OR instead of Math.floor:

var bname = 'banners/banner_'
+ ((Math.random()*15 | 0) + 1)
+ '.jpg';

preload=new Image();
preload.src=bname;
document.getElementById('rndimg').src=bname;
Here you immediately load the image into the banner, you've given 'preload'
less than a millisecond to load and cache the image file. Also, it seems
you are running this onload so the image doesn't start loading until the
entire rest of the page has finished, making it more like a post-load
function. The function does not start downloading the image until it is
executed.

You are probably better to use a default image if scripting is not
available and replace the src with the random src using script:
<img src="noScriptDefault.gif" alt="" name="bannerImg">

<script type="text/javascript">
if (document.images && document.images['bannerImg'])
{
document.images.bannerImg.src = 'banners/banner_'
+ ((Math.random()*15 | 0) + 1)
+ '.jpg';
}
</script>
Presumably the banner is right at the top, so it should load very near the
start anyway so any kind of preload is (almost) pointless. If you have
lots of script and CSS, then maybe to a preload right after the title
element will have a small benefit.

But the effect of multiple simultaneous connections will dilute the value
of pre-loading anyway. It was mostly useful for getting small button
images to load before big banners, but here you want to load the banner
first, which happens anyway.

Test and see.

}

banners are all named sequentially, 'rndimg' is the blank header image
having its source changed.

--
Rob
Mar 21 '06 #2
unfortunately server-side randomising isn't an option in this case.
however i used a modified version of your code snippet in the
individual pages after the image appears and it seems to work a treat.

many thanks for the help.

Mar 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Magic-chef | last post by:
This rollover script is created by Image Reaady. When using IE and especially with Win XP the images involved in the rollover disappear. It happens gradually with one or two at first then all. ...
4
by: David | last post by:
Hi everyone, I am trying to stop an image preload sequence by the click of a mouse but have been unsuccessful trying several methods. Imagine this simple script below that loads 50 images to...
2
by: sean.f.duffy | last post by:
Hi! I am an effectivebrand.com toolbar user and would love to add a preloader to my toolbar, but am having problems doing this. Is it possible to have javascript look at the URL of a webpage,...
8
by: Ross | last post by:
This page I am trying to create a preloader that hides all the other content except the hidepage div until it loads...any ideas? Or any preloaders that actually work? ...
3
by: abm | last post by:
Hi. I am in great need of a preloader for my Flash web site. I have looked at a lot of tutorials and examples, but I can not seem to get it to work. How do I add a preloader to my existing Flash...
2
by: sachaburnett | last post by:
Hi everyone! I'm new to Javascript and am finding so much useful information on this group, so thanks to you all! I have a question about preloading images for onmouseover/out effects and...
0
by: Lanky | last post by:
I have noticed on sites that on a page a preloader will run and one or more flash movies will appear. Then the preloader does not reappear on subsequest revists to that page during the same browser...
2
by: streammalvern | last post by:
Hi, I am having a problem with a published Flash Movie. I included a preloader and the whole file size of the .swf is only about 1MB. If I go to the movie, it pauses even before the preloader...
8
donilourdu
by: donilourdu | last post by:
hi, I want to preload the image on page load using javascript by, Doni
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.