Starting JavaScript Part 3:Triggered Scripts and FunctionsAs we mentioned earlier in part two, scripts are run either automatically as the web page is loaded, or they can be triggered. A triggered script is executed when something happens -usually when a button is clicked, or when the mouse passes over some part of the page. The following illustrates a script triggered when a link is clicked. In this case clicking the link causes an alert which displays the date. When you click ok on the alert you are taken to the link in this case a page called date.htm.
Click here and the script above will run in the frame below. Click here to reset
JavaScript FunctionsA function is code which is used more than once within a JavaScript program. Functions are called from the main program which is contained within the HTML and are normally placed between the header tags at the top of the HTML document. Regardless of where you place them they must be placed before the code that tries to use them! The code within the function is only executed when called and not when the page loads. Here in this example the function (green highlight) adds two numbers together and is called from within the main document (yellow highlight).
External Resources: |
||||
|