Creating Effective Functions!
Now that you've had a chance to write your first function and have practiced how to use events to trigger functions, you're ready to delve a little deeper.
The best way to think of functions is as a group of statements that perform a particular job. This can be a simple job requiring no more than one statement (as we saw previously) or a more complex function requiring many statements. Either way, functions perform a particular job.
So, if you return to the interactive story example, you can tidy this up and make it more user friendly by putting all the script into a function and using the onClick event on a button to trigger it off (see Figure 8.3):
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> ...
Get JavaScript™ 1.5 by Example now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.