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:
JS code
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.
getM();
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:
var count = 0;
function setCount()< /p>
{
count++;
if( count>=5 )
count = 0;
}< /p>
In this example, the
The attributes in the tag that respond to an event are called event handlers, and their values ??are JavaScript codes.
Common event handles:
onload:
Triggered when the web page is opened. It only works within