Current location - Quotes Website - Signature design - There are several forms of executing javascript code
There are several forms of executing javascript code

JavaScript definition method and JavaScript execution method

JavaScript definition method:

There are two methods for adding JavaScript code to HTML documents: embedded and linked. .

Embedded:

Embed JavaScript code in the HTML document. Method:

JavaScript code must be defined Between .

The src attribute is used to specify the address of the linked js file. It can be a local file or a URL.

Using linking, the defined JavaScript code can be shared by multiple web pages.

JavaScript execution method

There are two execution methods for JavaScript code defined in HTML documents: direct execution and event-driven.

Direct execution:

When we open a web page, all JavaScript code defined in the

The above JavaScript code is placed naked in the

The above JavaScript code defines a function. When the web page is opened, the code in the function will not be executed. When you need to execute this function, you need to use a function call.

Event driven: < /p>

When a certain event occurs, such as a web page being opened, a mouse click, a double-click, etc., a certain piece of JavaScript code is executed.

For example:

In this example, the