JS advantage in an external file

JS advantage in an external file

Js code embedded in HTML, although there is no problem, but it is generally considered preferable to use an external file containing codes. However, there is no mandatory requirement must use an external file, but supports the use of external files more people will emphasize the following advantages:

  1. Maintainability: across different HTML pages js code would cause maintenance problems, but all the js files in a folder, maintain it so much easier. And developers can without touching HTML tags and focus edit js code.
  2. Cacheability: The browser can go to all of the external files depending on the cache link. That is, if there are two pages use the same file, then the file is downloaded only once. Therefore, the end result is the ability to speed up page loading.
  3. Adapt to the future: to include an external js file without using XHTML or comments hack mentioned earlier. HTML syntax and XHTM include external files is the same.

The JavaScript inserted into an HTML page to use <script>elements. Use this element JavaScript can be embedded into an HTML page, the script and marks are mixed together; may also include an external JavaScript file. And where we need to pay attention to are:

  1. When including external JavaScript file, you set the src attribute to point to a file must be url. And this document may be the containing page files located on the same server, it can be other files in the domain.
  2. All <script>the elements will in turn be resolved in the order they appear on the page. Without the use of defer, async attribute, only after parsing the front <script>after parsing the code will start later in </script>the code elements.
  3. Because the browser will first finish resolve not to use the defer attribute of <script>the code elements, and then parse the contents of the back, so in general should put <script>elements on the back end, and the main content of the page, </body>the front label.
  4. Use defer attributes can then execute the script after the document fully rendered. Delay script is always executed in the order they are specified.
  5. Use ansyc property may represent the current script, without waiting for other scripts, do not clog document rendering. We can not guarantee asynchronous script executed in the order they appear on the page.
  6. In addition, <noscript>the elements can specify alternate content displayed in the browser does not support script in. But in the case of a startup script, the browser does not display <noscript>any content elements.
Published 27 original articles · won praise 5 · Views 6112

Guess you like

Origin blog.csdn.net/systempause/article/details/104325784