<html>
<head>
<script language=JavaScript>
function mouseOver()
{
document.images["myImage"].src = "Img2.jpg";
}
function mouseOut()
{
document.images["myImage"].src = "Img1.jpg";
}
</script>
</head>
<body>
<A style="cursor : default" href="" onclick="return false" name="link1"
onmouseover="mouseOver()" onmouseout="mouseOut()">
<img src="Img1.jpg" name="myImage">
</A>
</body>
</html>
Save this as ch05_q3.htm.
At the top of the page we define our two functions to handle the onmouseover and onmouseout events.
function mouseOver()
{
document.images["myImage"].src = "Img2.jpg";
}
function mouseOut()
{
document.images["myImage"].src = "Img1.jpg";
}
The function names tell us what event they will be handling. We access the img object for our <img> tag using the document.images array and putting the name in square brackets. In the onmouseover event we change the src property of the image to Img2.jpg, and in the onmouseout event we change it back to img1.jpg, the image we specified when the page was loaded.
In the page itself we have our <img> tag inside an <A> tag.
<A style="cursor : default" href="" onclick="return false" name="link1"
onmouseover="mouseOver()" onmouseout="mouseOut()">
<img src="Img1.jpg" name="myImage">
</A>
We need to do it this way so that our pages work with NN 3 and 4. Only IE 4/5/6 and NN 6/7 support the onmouseover and onmouseout events for an img object. However, NN 4 does support these events for the <A> tag's A object, so we use this workaround to get the result we want.
Note that I've also added onclick="return false". Why? By returning false, the click event is canceled; remember this link is here for its events only and not as a hyperlink. Why not leave the href="" out altogether? Well, while this will work fine in IE, it won't in NN, which ignores events if no href is included. Finally, for IE only, I've set the cursor style when the mouse pointer is over the link to the default cursor rather than a hyperlink mouse cursor.
style="cursor : default"
|