One, JavaScript can be written inside the HTML page, through the <script> tag, such as the bold part of the following code:
<!DOCTYPE HTML><HTML> <Head> <MetaCharSet= "UTF-8"> <title></title> <script type= "Text/javascript"> console.log ( " Hello world! " ); </ script > </Head> <Body> </Body></HTML>
Second, JavaScript can also be written in a separate file, with ". js" as the suffix, the HTML language through the <script> tag's SRC attribute introduced, such as the following code in the bold section:
JS folder to create the new Javascript0.js file:
Console.log ("Hello world!");
The HTML code is as follows:
<!DOCTYPE HTML><HTML> <Head> <MetaCharSet= "UTF-8"> <title></title> <Scripttype= "Text/javascript">Console.log ("Hello world!"); </Script> <script src= "js/javascript0.js" type= "Text/javascript" charset= "Utf-8"></script> </Head> <Body> </Body></HTML>
The Bold section is the introduction of the Js/javascript0.js file, which has the same effect as writing an HTML file, with two benefits:
1. Code Reuse
2. Clear structure
Two points should be noted here:
1. In the JS file does not need to write <script> tags, the tag is to tell the browser engine, the tag inside the JavaScript code, and the JS file has shown that this is the JavaScript code.
2. It is not possible to write JavaScript code in the <script> tag that introduces external JavaScript files, even if writing does not work.
The JavaScript code is executed in the order of the HTML. In general, JavaScript is written at the end of the file, or in a JavaScript method, which is only known after the page is loaded. There are two benefits to doing so:
1. Increase the page load speed, because the JavaScript code executes, the following HTML code will not be rendered, in addition, the JS file download will take time. Improve the user experience.
2. The DOM that prevents JavaScript code from operating has not yet been loaded. Cause an exception.
JavaScript Learning Notes (3) How--javascript and HTML are combined