473,769 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript" type="text/JavaScript">

<!-- Preload images

if (document.image s) {
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="bu tton1.src=image on.src;"
onmouseout="but ton1.src=imageo ff.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="preLoad er();">. 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 2984

sa**********@ya hoo.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="JavaS cript" 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.image s) {
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="bu tton1.src=image on.src;"
onmouseout="but ton1.src=imageo ff.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="th is.src=imageon. src;"
onmouseout="thi s.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="preLoad er();">. 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="th is.src=image1on .src;"
onmouseout="thi s.src=image1off .src;">
</a>

<a href="doc/doc2.htm">
<img src="/images/image2off.gif
onmouseover="th is.src=image2on .src;"
onmouseout="thi s.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
4445
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 on the image (it is a link to the content page) it changes to a third image AND STAYS THERE. ie: so the iamge won't change back to the omouseoff image. Does this make sense? Also, how do I "preload" the images before they are even needed? eg:...
2
9179
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 in the status bar 'Symantec Corporation' whenever anyone mouses over (onMouseOver) my image or link OR when one clicks while holding the left mouse down (onClick) on the same image or link. Upon releasing the mouse (onMouseOut), the
9
2048
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 one. Maybe I am just getting blurry-eyed, but can someone help me figure out what I have done wrong? Thanks!!!! <td width="190" height="57"><a href="Trends.htm" onmouseover="chgImg('Trends','InteriorTrendsBold')"...
2
2522
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 user clicks on the 2nd tab, the image changes so that the 2nd tab is highlighted a different colour, and so on. The image for the 1st tab is displayed initially, and the other 3 are preloaded using standard Macromedia Dreamweaver JavaScript...
4
1693
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() tester.onload=function() {cImg('good')} tester.onerror=function() {cImg('bad')} tester.src="image.jpg" } function cImg(typ) {
3
1543
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"> function option1() { document.getElementById("menu").write("<img src=m1.gif>"); } function option2() { document.getElementById("menu").write("<img src=m2.gif>"); }
6
24190
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 mouseout are working perfect but only the source is not setting using java script. Please clarify me regarding this. PS: This problem is only for safari browser. <tr>
62
17852
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(); img.src = ; Then I use the image a few more times by adding it into an Array object:
11
3014
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 width="50%"></TD> <TD width="110"><a href="index.php"><IMG SRC="{image-path}matrix_03.gif" WIDTH=110 HEIGHT=40 ALT="Homepage" border="0" onmouseover="myOn('matrix_03')" onmouseout="myOut('matrix_03')"></a></TD> <TD width="100"><a...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.