JS load execution order in HTML

Source: Internet
Author: User
Tags cdata

1. Loading order: Introduce the order of the <script/> of the tag, load it sequentially

The JavaScript code on the page is part of the HTML document, so the order in which JavaScript executes when the page is loaded is the order in which the markup <script/> is introduced.
The external JS, either inside the <script/> tag or introduced by SRC, is executed in the order in which the statements appear, and the execution process is part of the document loading.

2. The global variables and functions defined by each script can be called by the script executed later.

The call to a variable must be previously declared, otherwise the value of the variable obtained is undefined.

<script type= "Text/javscrpt" >//<![ cdata[    alert (TMP);       // output undefined var     TMP = 1;    Alert (TMP);   // output 1//]] >  </script>  

3. The same script, the function definition can appear after the function call, but if it is in two pieces of code, and the function call in the first code, the function does not define the error.

<script type= "Text/javscrpt" >//<![ cdata[    aa ();            // Browser error//]] >  </script>  <script type= "text/javscrpt" >//<![ cdata[    aa ();   // Output 1     function AA ()    {      alert (1);    } // ]]>  </script>  

4, the same name JS function execution order

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> functionAA () {alert (' First AA ')      }     </script> <title></title> functionAA (s) {alert (' Second AA '); }      functionAA (s) { executed alert (' Last AA '); }  </script> 

executes the last function with the same name , so the result is click "Botton" to execute the result: End AA

5.document.write () writes the output to the location of the script document, and after the browser resolves the contents of the document Documemt.write (), it continues to parse the contents of the document.write () output, after parsing is complete
Then continue parsing the HTML document.

<script type= "Text/javascript" >//<![ cdata[        document.write (' <script type= ' Text/javascript "src=" test.js "></' + ' script> ') ;    document.write (' <script type= ' text/javascript ' > ');  document.write (' Alert ' ("2"); ' ) ;       document.write (' alert ' ("I am" + tmpstr); );        document.write (' </' + ' script> ');     </script>     <script type= "Text/javascript" >  Alert (3);   </script>
Test.js File Contents
var tmpstr=2;
alert (TMPSTR);

Order of Execution 2 2 I am 2 3

6. js in the introduction of JS file must be aware of the <script></script> separation import, otherwise it will truncate the main file JS, mainly behind the </script> end tag

document.write ("<SCR"+"IPT src= ' my.js ' ></scr"+"ipt> "
Or
document.write ('<script type= ' Text/javascript "src=" test.js "></'+'script> ');

JS load execution order in HTML

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.