Beginner after a little help : Roll over menu images

M

Marcus

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
 
Y

Yann-Erwan Perio

Marcus said:
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.
 
M

Marcus

Yann-Erwan Perio said:
Marcus said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top