instructional systems
Index:
[Session11] JavaScript 1 (Interactive page)
1 Chapter1
2 Chapter2
3 Chapter3
--3.3 Application of Handling
4 Chapter4
5 Chapter5
Your Location: Home Page  >  [3] Interactive web pages using JavaScript (on condition)  >  [Session11] JavaScript 1 (Interactive page)  >  Chapter3  >  3.2 Definition of Handling
Definition of Handling
[The purpose of this section]
Learn the basic rules for opening JavaScript and making definitions.

Contents of js File -- Various Rules

Next we will learn the [Definition of handling] using our prepared file.

First download sample.zip file. (This should contain a JavaScript file named “myscript.js” and an HTML file named “sample.html”.)

Note: An unlimited number of handling definitions can be made in a file. Therefore, it would be convenient to define various handling items in a single file. Of course you don’t need to use them all when you are to use only some of them. (Note that four handling items are defined in the file you just downloaded.)

When you open myscript.js using Hidemaru Editor, you should see that the top portion would look as follows.

//---------------------------------------------------
// A message is displayed in a new window.
//------------------------
function oshita() {
  window.alert('Thank you very much. Have a good day!');
}
//---------------------------------------------------

In JavaScript, two slashes “//” means that everything thereafter to the end of the line is a comment. Therefore, the above top three lines and the last line are the comment lines. These explain the contents of the defined handling, and the scope from where to where is the definition.

The handling in JavaScript is usually defined as following.

function handling name (input 1, input 2, ...) {
 handling 1;
 handling 2;
  .......
  .......
}

To define handling, write “function” first, followed by the arbitrary handling name you defined. In the previous example, “oshita” is the handling name.

Next describe the input (generally called argument) necessary for the handling, enclosing it in parentheses “( )”.When there are multiple inputs (arguments), separate them by commas, “,”.Even when no input (arguments) are necessary, you cannot omit parentheses “()” . The handling “oshita” shown earlier is a handling that does not need an argument.

(Note) Handling is usually called “method” or “function.”

Lastly, describe the contents of handling to be defined, with the entirety being enclosed by “{ }”. At the end of each handling, put a semicolon “;”. In the example of “oshita”, a single handling of window.alert() is to be executed once.

Copyright (C) Kenichi Sugitani, Hideki Matsuda, Chisato Noguchi and Fumiko Ryu 2005-2006, All Rights Reserved