Advanced JavaScript programming 3------ (Chapter II) using JavaScript in HTML

HTML4.01 for the label defines the following six attributes:

  1. async: Optional. It indicates you should download the script immediately, without prejudice to other actions. Other resources such as downloading or waiting to load other scripts. Valid only for external scripts
  2. charset: Optional. It represents the code specified by the src attribute of the character set. The browser will ignore the general, basic is not used.
  3. defer: Optional. It indicates that the script can be delayed until the document is completely parsed and displayed after the execution. Valid only for external scripts. Is equivalent to a script in effect at the end of the body, can be substantially destroyed. Of course, if your own programming practice is to put together all the scripts, you can add some post script to this property! ! !
  4. language: Obsolete. Identifies the language of the script.
  5. src: Optional. It represents the external file that contains the code to be executed path or link!
  6. type: Optional. Be regarded as an alternative language of property. Example: type = "text / javascript" can be ignored, can not write.

Which can be said to these commonly used. defer Dynasty can use it; if not able to determine if, by deferalways than asyncstable. . .

Precautions:

  1. When using the <script> tag to embed js code, please do not re-emerge code ( '</ script>') tag, the browser will mistakenly believe that the end of the code. Solution: '<\ / script>' escape solution
  2. Note that for the script tag with src of Never write js code, because it will only download external script code, which will ignore the intermediate code, remember! ! !
  3. In order to avoid js before loading the script, page structure has emerged, generally recommended js file content label on the back of the body. Of course, also be placed in front, on which the label applied to defer attribute name.
  4. Reality, delay script (defer) does not always follow the script execution, execution will not necessarily DOMContentLoaded before the event, it is better to include only an external asynchronous script.
  5. For async and defer, are suitable for external script injection, but defer tie will be performed in sequential order; async there is no guarantee can be performed in order between them, suggested the recommendation defer.
  6. If there are two or more asynchronous script, when using async ensure that no dependencies between them, and not the page is rendering operations before dom

 The advantages of an external js script introduced:

  1. Maintainability. All the js in a folder, maintain it so much easier. And developers can without touching the page file structure to focus on editorial changes js code.
  2. Cacheability. Browser cache can go to an external script file links depending on the setup. Popular to say that when there are two pages share a script file that can be downloaded only once. That can speed up the loading speed of the page.
  3. Adapt to future trends. For example, external files and html xhtml way of introducing the syntax is the same.

 noscript tags:

  1. Browser does not support script
  2. Browser support script, but the script is disabled 

Meet the above two conditions, the noscript content will appear.

The purpose solution: When the browser does not support js script language, so pages gradual degradation

Published 163 original articles · won praise 31 · views 40000 +

Guess you like

Origin blog.csdn.net/COCOLI_BK/article/details/103503961