Note: This is the second chapter in the relearning JavaScript advanced Programming series.
About “relearning JavaScript advanced programming” is a review of the basics of JS learning.

1. How do I use JavaScript in a page

The main way to use JS in HTML today is through page inserts

Internal use of elements:

<script type="text/javascript">
    alert('abc')    
</script>
Copy the code

Matters needing attention:

1. Code contained in a script is executed from top to bottom

2. It is worth noting that the rest of the page will not be loaded and parsed during the parsing of the JS code until the js section is completed.

3, in the process of writing, pay attention to the script code block, do not appear before the code closing tag, this will lead to code errors.

4. The closed tag can be omitted when referring to JS externally, but in order to perform correctly in IE, it is recommended not to be omitted

5. If you use SRC to import external JS, there can be no more embedded JS blocks between script tags

6. Be careful when referencing outfield JS files by SRC, as this may cause security problems by replacing the js file.

7. Javascript blocks are executed sequentially on the page, and only after the first one has been executed will the subsequent ones be executed sequentially.

2. Label position

Generally in accordance with the practice of the introduction of js code should be placed, but the js code page when performing other elements will not be able to be loaded this will result in the page interactive is very poor, so now generally put js code before the closing tag, after all the HTML page, so can’t because of js load and impact load of the page.

<html>
    <head>
        <title>demo</title>
    </head>
    <body>
        <script type="text/javascript" src="a.js"></script>
    </body>
</html
Copy the code

3. Defer the script defer async

Defer: The script does not affect the construction of the page as it executes because js execution is delayed until the page is fully loaded

Async: Scripts and page loads are executed synchronously.

4. To summarize

There are two ways to use Javascript in a page, either by introducing external scripts via SRC or by inserting JS code between script tags.

1. Both methods require setting type to text/javascript

2. When external JS files are included, SRC must be set to the url pointing to the corresponding file

3. All script code is executed in the order in which it appears on the page

The browser must parse the script code before rendering the content, so be sure to place the script code at the end of the page

Welcome to my official account [Xiaoyao students]