JavaScript Functions


 

JavaScript functions explained
JavaScript variables explained
JavaScript handlers explained
JavaScript parameters explained
A Tutorial about the rollover code
Frequently asked questions

 

 

If you are familiar with other computer languages then you may have already encountered functions. Functions are basically pieces of JavaScript that can be reused as many times as you wish. An example of where a function could be used is in a web page that contains 5 rollover images. This would require a script that swaps the images when the user rolls their mouse over them (this is looked at further in the tutorial).

Without functions, the only way of doing this would be to paste the same rollover script in for each image, meaning a long, ugly script - time consuming to create and difficult if you need to update the script in any way.

With functions, each rollover image will use the same script. The function will be contained in the <head> tag of the HTML document and will be 'called' each time the user rolls over one of the images.

Here's an example of a simple function
  <HEAD>
  <SCRIPT>
  function Welcome()
	  {
   	  alert("Hello from the Web Genie")
   	  }
  </SCRIPT>
  </HEAD>

  <BODY>
	  <FORM>
	  <INPUT TYPE ="button" VALUE ="Click Me" onClick="Welcome()">
	  </FORM>
  </BODY>
Click on the button to see what the function does