JS Advanced three basic articles (i)

Source: Internet
Author: User

It is not easy to get to the beginning, I did not know what to write or record the highlights of their discoveries:

1, it is recommended to put all the introduced JavaScript in front of the </body> end

2, <script> defer (deferred) with HTML5 async (async)

Defer: Add defer = "defer" in external JS for example: <script type= "Text/javascript" src= "jquery-1.8.3.min.js" defer = "defer" ></sc Ript> scripts are deferred until the entire page resolution is complete and then loaded but in reality the delay scripts do not necessarily follow them in the order in which they are loaded or not necessarily loaded before domcontentloaded: It is best to include only one deferred script HTM5 ignore the properties defer the embedded script settings ; IE8 >= fully supports HTM5-mandated behavior

Async: Set async = "Async" This property is similar to defer but unlike defer, the script that marks async does not guarantee execution in the order specified. Async must be executed before the Load event, but may be executed before or after the domcontentloaded event.

Under normal circumstances, The domcontentloaded event is executed before Window.onload, and the domcontentloaded event is executed when the DOM tree is built, and window.onload is executed when the page is loaded, including elements such as pictures. Most of the time we just want to bind events to elements after the DOM tree is built, we don't need picture elements, and sometimes it's very slow to load other images.

3, usage in XHTML: Unlike HTML, XHTML is sensitive to capitalization,<title> and <TITLE> are different labels. XHTML requires that all labels and attribute names must be lowercase.

<script type= "Text/javascript" >

function Compare (A, b) {
if (A < b) { return ' A is less than B '; } Else if (a>b) { return ' a greater than B '; } Else {  return } }

    </script>

The code is as follows:

  

 <script type= "Text/javascript" > function   compare (A, b) { if  (a &LT; b) {             return  ' A is less than B ' ;  else  if  (A>B) { return  ' A is greater than B '  else  { return  ' A = = B '  </script> 
This can be used to cause incomprehensible , CDATA fragments to contain JavaScript code; CDATA Fragments in XHTML and XML are a special area in the document; The introduction of CDATA fragments is as follows:
<script type= "Text/javascript" >functionCompare (A, b) {<![cdata[if(A <b) {                    return' A is less than B '; }Else if(a>b) {                    return' A greater than B '; }Else{return' A = = B '                }                }            ]]>//There are also those that don't support CDATA fragments that comment out the CDATA markup            functionCompare (A, b) {<![cdata[if(A <b) {                    return' A is less than B '; }Else if(a>b) {                    return' A greater than B '; }Else{return' A = = B '                }                }            ]]> </script>

4

<noscript>
For unsupported JS display
</noscript>

JS Advanced three basic articles (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.