JavaScript Getting Started 3 --------------- Jilin University CAE Campus Fee Forum Faith 2008

xiaoxiao2021-03-06  48

The basic composition of the JavaScript scripting language is programmed by control statements, functions, objects, methods, attributes, etc. to implement.

First, the program control flow

In either language, the program control flow is necessary, which makes the entire program reduce confusion, making it performed smoothly in a certain manner. Below is the program control flow structure and statement commonly used by JavaScript:

1, IF condition statement

Basic format

IF (expression)

Sentence section 1;

......

Else

Sentence section 2;

.....

Function: If the expression is TRUE, the segment segment 1 is executed; otherwise the segment segment 2 is executed.

Description:

The if -else statement is the most basic control statement in JavaScript, which can change the execution order of the statement.

The relationship statement must be used in the expression to implement judgment, which is estimated as a boolean value.

It transforms zero and non-zero numbers to false and true.

If the statement after IF has multiple lines, you must enclose it using the curneth.

IF statement nested

IF (Boolean) statement 1;

Else (Boolean) statement 2;

Else if (Boolean) statement 3;

......

ELSE statement 4;

In this case, the Boolean expression of each stage will be calculated. If it is true, the corresponding statement is executed, otherwise the ELSE is executed.

2, for loop statement

Basic format

For (initialization; condition; increment)

Statement set;

Function: Implement the conditional cycle, when the condition is established, the statement set is executed, otherwise the cyclic body is jumped out.

Description:

The initialization parameter tells the beginning of the cycle, and the initial value of the variable must be given;

Condition: It is a condition for discriminating the cycle stop. If the condition is met, the cyclic body is executed, otherwise it is jumped out.

Incremental: Main Defining Cyclic Control Variables Changes in what ways during each cycle.

Between three main statements, comma separation must be separated.

3, WHILE cycle

Basic format

WHILE (Condition)

Statement set;

The statement is the same as the For statement, and when the condition is true, repeat the loop, otherwise it will exit the loop.

For with while statement

Both statements are cyclic statements, using the for statement, more easy to understand when processing numbers, and is more compact; while the While cycle is more special.

4, BREAK and Continue statements

As in the C language, use the BREAK statement to make the loop from the For or While out, Continue enters the next loop in the loop from the FOR or WHILE.

Second, functions

The function provides programming staff with a full-constant ability. When a complex program is designed, the program is always divided into some relatively independent portions based on the functionality to be completed, and one function is written. Thus, each part is fully independent, the task is single, clear, easy to understand, easy to read, and easy to maintain. The JavaScript function can encapsulate modules that may be used multiple times in the program. And can be called as an event-driven result. Thus, a function is implemented to associate it with the event driver. This is a place with other languages.

1, JavaScript function definition

Function function name (parameter, zero) {

Functional body;

Return expression;

}

Description:

When the modulation function is called, the variables used can be transmitted as a variable. The function is defined by the keyword function.

Function name: Define the name of its own function.

The parameter table is the value passed to the function or action, which can be constant, variables, or other expressions.

Call a function by specifying a function name (inform).

You must return the value using Return.

The function name is sensitive to case.

2, form parameters in the function:

In the definition of a function, we have a parameter table after the function name, which may be one or more variables. So how can I determine the number of parameter variables? In JavaScript, you can check the number of parameters via arguments .length. example:

Function function_name (exp1, exp2, emp3, eXP4)

Number = function _name. Arguments .length;

IF (Number> 1)

Document.wrile (EXP2);

IF (Number> 2)

Document.write (EXP3);

IF (Number> 3)

Document.write (EXP4);

...

Third, event driver and event processing

1. Basic concept

JavaScript is a language based on object-based. This is different from Java, Java is an object-oriented language. Based on object-based characteristics, event-driven is employed. It is in an environment in which everything is simplified. Usually the action of the mouse or hotkey is called event (Event), and a series of programs caused by the mouse or hotkey, called event driver. For the event, the event is handled or function, we call an event handler (Event Handler).

2, event handler

The processing of object events in JavaScript is usually served by a function (Function). Its basic format is the same as the function, and all functions described above can be handled as an event handler. The format is as follows:

Function event processing name (parameter table) {

Event processing statement set;

......

}

3, event driver

The event in the JavaScript event drive is caused by the movement of the mouse or hotkey. It mainly has the following events:

(1) Click Event Onclick

When the user clicks the mouse button, an OnClick event is generated. At the same time, the event handler or code specified by ONClick will be called executed. Usually produced in the following basic objects:

Button (Button Object)

Checkbox (check box) or (check list box)

Radio (single button)

Reset Buttons (Important Button)

Submit Buttons (Submit button)

Example: You can activate the Change () file by the following buttons:

After the OnClick equal sign, you can use your own function as an event handler, or a function in JavaScript can be used. You can also use JavaScript code. example:

This event is sent when the character value is entered using the text or Texturea element to change the event, and the event will also be raised after an option status in the SELECT table item.

Example:

(3) Select events Onsect

When the text in the Text or TextArea object is high, the event is triggered.

(4) Get focus event onfocus

This event is generated when the user clicks on Text or TextArea and SELECT object. This object becomes a front desk object.

(5) Lost focus onbrur

When the Text object or TextArea object and the SELECT object no longer have a focus, when retreats to the background, he triggered the file, and his with the ONFOCAS event is a corresponding relationship.

(6) Load file online

This event is generated when the document is loaded. Onload is a value that detects the value of the cookie when it is first loaded into a document, and assigns a variable to it, allowing it to be used by the source code.

(7) Uninstall file onunload

Running an ONUNLOAD event when the web page exits, and update the status of the cookie.

Fourth, sample

Example 1: The following example is an example of an automatic loading and automatic uninstallation. That is, when the LoadForm () function is called when the HTML document is loaded, the unloadform () function is first called when exiting the document into another HTML document, confirming that it can be entered.

TEST3_1.HTM