Address: http://www.qeefee.com/js/use-javascript-in-html
For more JavaScript tutorials, visit> JavaScript tutorials
Body:
You can use JavaScript on an HTML page in either of the following ways:
Embedded Javascript Code
In this way, JavaScript code is directly written in HTML. The corresponding usage is:
<! Doctype HTML> <HTML xmlns = "http://www.w3.org/1999/xhtml">
In the above Code, we put the script tag containing the JavaScript code in the header. When we run the page, a dialog box will pop up, such:
In addition to the header, JavaScript code can also be placed in the body:
<Body>
We add a script tag to the body and let it pop up a dialog box. When the browser loads this code, it will explain and execute it. The effect is as follows:
Both writing methods can be executed. There is no distinction between recommendation and not recommended writing, and there is no difference between correctness and error. It mainly depends on 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 loading the page to a tag, put it behind the corresponding tag.
Reference independent JS files
It is convenient to embed Javascript code in HTML tags, but it also easily leads to Page code confusion. Normally, we store JavaScript code in a JS file, then reference the JS file in HTML as follows:
It should be noted that loading external JS files is thread-blocking. When loading is not completed, the page will not continue to load the labels behind it, so we usually put the referenced JS behind the body.
Finally, download this example, hoping to help beginners!
Use javascript .rar