There are two ways to use JavaScript in an HTML page:
Embedded JavaScript code
In this way, the JavaScript code is written directly in HTML, and the corresponding usage is:
<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> <title>Using JavaScript in HTML-take-off web-javascript test page</title> <Scripttype= "Text/javascript">Alert ("Welcome to JavaScript-take-off network"); </Script></Head><Body></Body></HTML>
In the above code, we include the script tag of the JavaScript code in and head, and when we run the page, a dialog box pops up, such as:
In addition to being placed in the head, the JavaScript code can also be placed in the body:
<Body> <H1>Using JavaScript in HTML-take-off network</H1> <Scripttype= "Text/javascript"> //the Javascrip code embedded in the bodyAlert ("This is the JavaScript in the body-takeoff net"); </Script></Body>
We add a script tag to the body and let it pop up a dialog box that will be interpreted when the browser is loaded into the code, such as:
Both types of writing can be executed, there is no recommendation and not recommended to the writing of the points, but not correct and wrong, mainly to see your specific usage: If you want the page to start executing JavaScript code at the beginning of loading, you should put it in the head If you want to execute JavaScript code after the page is loaded into a tag, place it behind the corresponding label.
Referencing a standalone JS file
It is convenient to embed JavaScript code in HTML tags, but it is also easy to cause page code confusion, usually, we will put the JavaScript code in a JS file, and then reference the JS file in HTML, the following wording:
It is important to note that the loading of external JS files is a thread blocking, when the loading is not completed, the page will not continue to load the tag behind it, so we usually put the reference JS behind the body.
Finally give the download of this example, hope to help beginners friends!
Takeoff nets-use JavaScript examples in HTML. rar
Using JavaScript in HTML