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

Beginner after a little help : Roll over menu images


I am after what is probably very basic help. Below is the HTML including the
bit of Java I am trying to understand why its not working. The idea is as
the mouse moves over and off the "button" image changes.

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<title>(Type a title for your page here)</title>

<meta name="GENERATOR" content="Arachnophilia 4.0">

<meta name="FORMATTER" content="Arachnophilia 4.0">

</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080"
alink="#ff0000">

<a href="messhall.html"
onMouseOver="document.images[1].src='MessHall_on.gif' ;"
onMouseOut="document.images[1].src='MessHall_off.gif' ;"><img
src="MessHall_off.gif" border="0" height="25" width="150"></a><br>

</body>

</html>

What I get is the image in the "off" colour mode, but it doesn't change when
I roll the mouse over the image, will this work? What have I done wrong.

TIA,

Mark
Jul 23 '05 #1
2 2345
Marcus wrote:
Below is the HTML including the
bit of Java I am trying to understand why its not working.
Java and javascript are two different languages, platforms etc. They
have nothing in common apart their first 4 letters, so leave java alone
and let's concentrate on javascript:-)

[images rollover]
<a href="messhall.html"
onMouseOver="document.images[1].src='MessHall_on.gif' ;"
onMouseOut="document.images[1].src='MessHall_off.gif' ;"><img
src="MessHall_off.gif" border="0" height="25" width="150"></a><br>


You weren't far, in javascript collections are 0-indexed, try using
document.images[0] instead of document.images[1].
Your way of changing images is probably the most supported, but remains
rather old and hard to maintain; the "rollover" handler is set upon a
link and not the image, because NN4 didn't support mouseover/mouseout
events on images.

If you can yourself determine a naming scheme for your images, then you
could probably do something like the following, which would facilitate
your maintenance and prevent the HTML from growing needlessly.

---
<img src="foo1_off.gif">
<img src="foo2_off.gif">
<img src="foo3_off.gif">

<script type="text/javascript">
function handler(evt){
var p=getImageParts(this);
this.src=p[1]+(p[2]=="on"?"off":"on")+p[3];
}

function getImageParts(img){
//[src, name, on/off, extension] or null
return (/^(.+_)(on|off)(\.[a-z]+)$/i).exec(img.src);
}

window.onload=function(evt){
var g, p;
for(var ii=0,i=document.images; ii<i.length; ii++) {
p=getImageParts(i[ii]);

if(p){
// add the handlers
i[ii].onmouseover=handler;
i[ii].onmouseout=handler;

// preload the alternate image
g=new Image();
g.src=p[1]+(p[2]=="on"?"off":"on")+p[3];
}
}
}
</script>
---

When the page has loaded, a function will iterate all images and add the
relevant mouseover/mouseout handlers; the handler determines
automatically which source is to be set, in regards of the existing source.

The getImageParts function uses "regular expressions" to parse the
source of the image (i.e. to analyze the source string) - here it
"splits" the string into four elements, contained into an array: full
source, image's name, on or off, extension.

Using this approach, you just have to put the javascript anywhere on the
page, and that's it - you can add as many images as you want, the
behavior is already defined for them.
HTH,
Yep.
Jul 23 '05 #2

"Yann-Erwan Perio" <y-*******@em-lyon.com> wrote in message
news:41**********************@news.free.fr...
Marcus wrote:
> Below is the HTML including the
bit of Java I am trying to understand why its not working.
Java and javascript are two different languages, platforms etc. They
have nothing in common apart their first 4 letters, so leave java alone
and let's concentrate on javascript:-)

[images rollover]
<a href="messhall.html"
onMouseOver="document.images[1].src='MessHall_on.gif' ;"
onMouseOut="document.images[1].src='MessHall_off.gif' ;"><img
src="MessHall_off.gif" border="0" height="25" width="150"></a><br>


You weren't far, in javascript collections are 0-indexed, try using
document.images[0] instead of document.images[1].
Your way of changing images is probably the most supported, but remains
rather old and hard to maintain; the "rollover" handler is set upon a
link and not the image, because NN4 didn't support mouseover/mouseout
events on images.

If you can yourself determine a naming scheme for your images, then you
could probably do something like the following, which would facilitate
your maintenance and prevent the HTML from growing needlessly.

---
<img src="foo1_off.gif">
<img src="foo2_off.gif">
<img src="foo3_off.gif">

<script type="text/javascript">
function handler(evt){
var p=getImageParts(this);
this.src=p[1]+(p[2]=="on"?"off":"on")+p[3];
}

function getImageParts(img){
//[src, name, on/off, extension] or null
return (/^(.+_)(on|off)(\.[a-z]+)$/i).exec(img.src);
}

window.onload=function(evt){
var g, p;
for(var ii=0,i=document.images; ii<i.length; ii++) {
p=getImageParts(i[ii]);

if(p){
// add the handlers
i[ii].onmouseover=handler;
i[ii].onmouseout=handler;

// preload the alternate image
g=new Image();
g.src=p[1]+(p[2]=="on"?"off":"on")+p[3];
}
}
}
</script>
---

When the page has loaded, a function will iterate all images and add the
relevant mouseover/mouseout handlers; the handler determines
automatically which source is to be set, in regards of the existing

source.
The getImageParts function uses "regular expressions" to parse the
source of the image (i.e. to analyze the source string) - here it
"splits" the string into four elements, contained into an array: full
source, image's name, on or off, extension.

Using this approach, you just have to put the javascript anywhere on the
page, and that's it - you can add as many images as you want, the
behavior is already defined for them.
HTH,
Yep.


Thanks :-)

Not much more I can say, maybe brilliant help :-)

Re:

Marcus

Jul 23 '05 #3

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

Similar topics

23
by: hedylogus | last post by:
I've just begun learning C++ and I'm trying to write a program to shuffle a deck of cards. I've succeeded....for the most part....but every now and then rand() produces duplicate random numbers...
4
by: TempMan | last post by:
I want this text field to always display a number variable. The variable "num" is defined in the head, how can I get a text box to display this varibale?? <input name="Balance" type="text"...
1
by: ::::x:::: | last post by:
Hi All, below is come code I'm writing for an assignment, and I'm stuck. What I want to happen: page load, user click on money money = bank bank is displayed in a text box I can get the bank...
4
by: Yuk Cheng | last post by:
<<<start index.htm>>> <html> <head> <script> function perform(action){ } </script> </head>
6
by: Leythos | last post by:
I have a site at http://www.bhlnet.net/opep, it's template site for testing, it uses roll-over images that are setup/controlled by javascript. I've just noticed that the pages seem to be stuck with...
4
by: Chris | last post by:
Hi all, I've built a site using ASP.NET (my first) and currently it has no content. The layout and design is done (only 4kb of images used). Testing it out on my webserver I notice a large load...
4
by: Ben | last post by:
Hi, I'm using images in my menu control. I have my menu setup based on this example: http://msdn2.microsoft.com/en-US/library/system.web.ui.webcontrols.menuitembinding.imageurlfield(VS.80).aspx ...
4
by: clairelee0322 | last post by:
Hello guys again! I am a C++ beginner and I am working on another project.. It's called Dice Lab with Loops.. unforunately I don't even know how to start the program.... Don't blame me for not pay...
1
by: CKich30 | last post by:
When viewing my page (siuehockey.net) in Firefox, it's setup how I want it to look. When i viewed it using IE, the menu at the top runs into the "Cougar Hockey/cougarhockey.net" at the top left....
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
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...

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.