I. <script> marks
The first method is to put <sript></script> directly between the head and script two tags (title below,
<Head>... <title>Demo</title> <Scripttype= "Text/javaScript "> /*The type attribute is often ignored. Script default is JS, so <script type= "Text/java script" > can be directly abbreviated to do <script>*/ ... </scipt> </head>
Embedded JS, may involve some conflict with the HTML judgment. Operators such as <>. Can be overridden by HTML entities. such as > for >,< <.
two. Referencing an outside chain
Write a good JS file, and then reference, in the JS file, do not need to add <script> tags.
<typescript "src=" Xxx.js "></script > //The internal JS code will be ignored.
It is recommended to use the second method. It has the advantages of convenient maintenance, cache, standardization and so on.
three. So here's the problem .
The script tag is loaded from top to bottom. The rest of the page is not displayed until the parser <script> element evaluates to the end of all JS code. How do I improve performance?
1.defer Property Lazy Loading
One method is to take advantage of the defer property of the script element. A surface script executes when it does not affect the construction of the page (only after the HTML content has finished loading). If you include in the
<deferarc= "Xxx*.js"></script>
When two defer exist, they are theoretically executed sequentially.
2. Asynchronous loading
Another property of script, Async, is similar to defer. Requires that the JS file be downloaded immediately without affecting the construction of the page.
<deferAsync arc= "Xxx*.js"></ Script >
But when there are two async, even theoretically there is no guarantee that the two are sequentially loaded.
3. The best way
Put <script> to the end of the HTML page (before </body>), and then refer to the outside chain-so that the file can be loaded better and faster.
4. A further question
When the browser does not support the script, or the browser JS script is disabled, how to make the page smooth degradation?
Add the NoScript code to the last part of the BODY element (this code is not displayed when JS is normal):
< NoScript > < P > This page requires a browser support (enable) JavaScript script. </ P > </ Nosript >
Added
Historically, JS can be written in HTML annotations. It can be considered as the "row form" of JS. <!--JS code//-->, now obsolete.
2nd two ways to invoke JS--using JavaScript in HTML