Simple use of functions in JavaScript

handling things "on the spot"

html/JavaScript explanation
<input type="button" 
value="example" 
onclick="var h='hi';alert(h);">
A button, that calls the "alert" function (which is predefined in JavaScript). The two JavaScript commands inside quotes are separated by semicolons.
var h='hi';alert(h);

is equivalent to

alert('hi');

<a href="javascript:alert('hello');">
<img src="pix/c9.jpg"></a>
<a href="javascript:alert('hello');">

is roughly (depending on browser versions) equivalent to

<a href="#" onclick="alert('hello')">

In modern browsers, it is best not to use the "javascript:" notation. Additionally modern browsers support events in image tags, so that nesting the image inside <a> is no longer needed.

  *

<img src="o1.jpg" name="wbs" 
onclick="wbs.src='o6.jpg'">
Note that the image has been named
"wbs" so that its source file can be
swapped through a command.

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.