Simple use of functions in JavaScript
handling things "on the spot"
moving actions into a function
command/output explanation
<script>function doit(){ alert("hello"); }</script> <input type="button" value="example2" onclick="doit()">The call to the alert message has been moved from an in-line call to a user-defined function "doit( )".
<script> Web1=new Image() Web1.src="o1.jpg" function swap(){ if (wb2.src==Web1.src) wb2.src="o6.jpg" else wb2.src="o1.jpg" } </script> <img src="o1.jpg" name="wb2" onclick="swap()">Moving an action to a script may require more formal naming of the objects involved. See if you can determine why the Image object Web1 has been created.
<script> function swap(I){ document.images[I].src=="o6.jpg" } </script><img src="o1.jpg" name="a" onclick="swap(0)"><img src="o1.jpg" name="b" onclick="swap(1)">Selecting which of two images to swap, based on the receipt of a parameter.
* Picture of Noah Webster taken from from Webster's New International Dictionary of the English Language, 1911, G & C Merriam Co. Springfield, MA.
The other picture is not a person -- just a drawing. It is not about Carly Simon nor Toby Keith, even if either should think it is.