Hi there,
First of all you need two files. Your buttons.js file and an html file we'll call test.html.
test.html can be as simple as this:
Code:
<html>
<head>
<script type="text/javascript" src="buttons.js"></script>
</head>
<body>
<a href="menu/Help.php" onmouseover="document.helpbutton.src = helpbuttondown.src " onmouseout="document.helpbutton.src = helpbuttonup.src"><img src="images/helpbuttonup.gif" width="200px" height="25px" border="0" name="helpbutton" alt="help button"/></a>
</body>
</html>
Points to note
- The "script" tag call your javascript (.js) file to be used in the html file
- The "a" tag puts an anchor around anything in between it.
- onmouseover and onmouseout are events, the function of which is (normally) written in javascript, which means that...
- "document.helpbutton.src = helpbuttonup.src" is javascript code. What it is saying is that the "src" value of an element called "helpbutton" somewhere in this document should now take the "src" value of something called helpbuttonup
- The "img" tag is placed in between the "a" or anchor tags. The 'name="helpbutton"' bit gives this img tag a name in the document - we have now defined our "helpbutton" element used mentioned in the anchor tags events!
All we need to do now is write some more javascript to finish the puzzle:
- What are "helpbuttonup" and "helpbuttondown"?
- What are their "src" values?
Therefore "buttons.js" can be as simple as:
Code:
helpbuttonup = new Image();
helpbuttondown = new Image();
helpbuttonup.src = "images/helpbuttonup.gif";
helpbuttondown.src = "images/helpbuttondown.gif";
Points to note
- helpbuttonup and helpbuttondown are defined as new images
- the src values for our new images can then take the value of the save locations of your actual images.
All that remains is to save everything in the right place. Keep the html and js file in the same directory and create a sub directory called "images" to store your pictures.
There are many different ways to achieve the same effect, but essentially all you are doing is dynamically changing the "src" attribute of your "img" element depending on an event. Once you understand the basics of what's going on, you can then use javascript even more dynamically to improve the readability and portability of your html code.
Hope this helps!