473,378 Members | 1,409 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,378 software developers and data experts.

Image preloading and onmouseover/out questions

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 found so many different ways to do it on the Net but am not sure
about something. Right now I have the following code inside my <head>
tag:

<script language="JavaScript" type="text/JavaScript">

<!-- Preload images

if (document.images) {
imageoff = new Image();
imageoff.src = '/images/imageoff.gif';

imageon = new Image();
imageon.src = '/images/imageon.gif';
}

//--></script>

Then in my html body I have:

<a href="doc/doc.htm"
onmouseover="button1.src=imageon.src;"
onmouseout="button1.src=imageoff.src;">
<img name="button1" src="/images/imageoff.gif">
</a>

Here are my questions:

- I notice that some people use a FOR loop to load mulitple images. Are
there any advantages in using a FOR loop as opposed to using multiple
"new Image ()" and ".src" as I did above?

- Do you need to use onLoad in the <bodytag in order for the images
to properly preload. I see some examples where all the images to be
preloaded are inside a function. So the image preload code I have above
would be inside a function (eg., preLoader() ) and then the <bodytag
would look like <body onload="preLoader();">. Will it still work if I
only have what I have above or do I need to use onload? (my images are
very small and my Internet connection is really fast so I don't don't
if the images are actually being preloaded).

Thanks SO SO SO much!

Sacha

Aug 26 '06 #1
2 2956

sa**********@yahoo.com wrote:
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 found so many different ways to do it on the Net but am not sure
about something. Right now I have the following code inside my <head>
tag:

<script language="JavaScript" type="text/JavaScript">
The language attribute is deprecated, so remove it. Keep type, it's
required.

<!-- Preload images
Do not use HTML comments inside script elements, use proper JavaScript
comment delimiters: // or /*...*/

if (document.images) {
imageoff = new Image();
imageoff.src = '/images/imageoff.gif';
It is best to explicity declare variables with 'var'. Here imageoff is
global, but use var anyway, it makes your intent clear. Many
programming style guides say to declare all your variables in one
place, usually at the start of the scope in which they are used. So
all your global variables should be declared at the start of the script
element:

// Images for rollover
var imageon, imageoff;

So you can easily add comments on what they are for and get
self-documenting code.

imageon = new Image();
imageon.src = '/images/imageon.gif';
}

//--></script>

Then in my html body I have:

<a href="doc/doc.htm"
onmouseover="button1.src=imageon.src;"
onmouseout="button1.src=imageoff.src;">
<img name="button1" src="/images/imageoff.gif">
Using element names or ids as global variables is an IE-ism that is not
well supported by other browsers. It is considered a rather poor
design decision and shouldn't be relied on. Here it isn't necessary at
all, put the mouseover/out on the actual image:

<a href="..."><img onmouseover="this.src=imageon.src;"
onmouseout="this.src=imageout.src;"></a>

</a>

Here are my questions:

- I notice that some people use a FOR loop to load mulitple images. Are
there any advantages in using a FOR loop as opposed to using multiple
"new Image ()" and ".src" as I did above?
Yes: less code and (probably) easier maintenance.

- Do you need to use onLoad in the <bodytag in order for the images
to properly preload.
No.

I see some examples where all the images to be
preloaded are inside a function. So the image preload code I have above
would be inside a function (eg., preLoader() ) and then the <bodytag
would look like <body onload="preLoader();">. Will it still work if I
only have what I have above
Yes.

or do I need to use onload?
No.

You should consider using CSS rollover buttons: use one image that has
the "on" and "off" images joined as one image. Use A: hover to slide
it left-right or up-down to show the other image, then back again when
the A is "unhovered". No pre-loading and no script. There is an
example here:

<URL:
http://www.search-this.com/website_d...r_buttons.aspx >

There is another one here:

<URL: http://www.alistapart.com/articles/taminglists/ >

If you have buttons or tabs of varying sizes, you might like the
sliding doors techique explained here:

<URL: http://alistapart.com/articles/slidingdoors/ >
(my images are
very small and my Internet connection is really fast so I don't don't
if the images are actually being preloaded).
Sounds prefect for rollover buttons. :-)

--
Rob

Aug 28 '06 #2
Hello Rob,

Thank you very much for your very informative advice! I had no idea
that using element names as global variables was an IE-ism --
especially since I'm a Firefox user!

I wasn't aware that I can put the onmouseover/out directly inside the
<imgtag. Does the "this.src" keyword refer to the image being
referenced?

So if I had mulitple images using the rollover effect, I would have:

<a href="doc/doc1.htm">
<img src="/images/image1off.gif
onmouseover="this.src=image1on.src;"
onmouseout="this.src=image1off.src;">
</a>

<a href="doc/doc2.htm">
<img src="/images/image2off.gif
onmouseover="this.src=image2on.src;"
onmouseout="this.src=image2off.src;">
</a>

.... and so on?

Sacha

Aug 28 '06 #3

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

Similar topics

2
by: Rob Manger | last post by:
Hi All, I am hoping someone can help me. I am trying to setup my main page so that when the user moves the mouse over an image, it changes the source (got this working). When the user CLICKS...
2
by: Kevin Lyons | last post by:
Hello, Can anyone assist me with what I am trying to do with the following code (six different scenarios to try to make the functionality work correctly)? I want to always (and ONLY) display...
9
by: Karl Burrows | last post by:
I am working on a Website for a non-profit group and for some reason I have one link that doesn't want to cooperate. All the image links work fine with the onmouseover and onmouseout script except...
2
by: Stevio | last post by:
In my web page I preload images so that when a user clicks on a part of the image, the image changes. The images are used to display 4 tabs. Each image shows a different tab as highlighted. If a...
4
by: Dennis Allen | last post by:
Hi. I have one image that has to show up in a html page several times. To save time, I'd like to preload this image. As an example: if (document.images) { tester = new Image()...
3
by: bzi999 | last post by:
Hi! I'm trying to create a javaxcript application that changes a image menu by following the mouse, something like: <!-- menu images are m1, m2, m3.gif--> <script language="JavaScript">...
6
by: abdullah1983 | last post by:
Hi Guys, I need some clarification regarding the problem with safari browser. Please find my code below. I'm setting the image src, mouseover and mouseout using javascript. The mouseover and...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
11
by: Evolution445 | last post by:
I got this code, and it somehow doesnt work. <TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=0 align="center"> <TR> <TD background="{image-path}navfiller.gif" HEIGHT=40...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.