On the sequence of JavaScript execution

Source: Internet
Author: User

  This article mainly introduces the sequence of JavaScript execution, and a friend in need can refer to the

JavaScript is the highest order of execution, unless you specifically specify that JavaScript code does not wait until the page is loaded. For example, a Web page contains the following HTML code:   Code as follows: <div id= "Ele" >welcome to www.jb51.net</div>   If you are in this line of HTML code before, Add the following JavaScript code: Code as follows: <script type= "Text/javascript" >   document.getElementById (' Ele '). Innerhtml= ' Welcome to my blog '; </script>   Run the page, you will get the error message: "document.getElementById (' ele ') is null. "The reason is that when the above JavaScript is running, there is no DOM element with the id ' ele ' on the page. There are two ways to solve this problem: 1. Put the JavaScript code behind the HTML code: the   code is as follows:  <div id= "Ele" >welcome to www.jb51.net</div> <script " Text/javascript ">   document.getElementById (' Ele '). Innerhtml= ' Welcome to my blog '; </script>   2. Wait until the Web page has finished loading to run the JavaScript code. You can use the traditional solution (load): First add HTML to the "<body load=" "Load ()" "," and then invoke the above JavaScript code in the load () function. Here's what you can focus on with jquery: The code is: <script> $ (document). Ready (function () {   document.getelementbyid (' ele ') . innerhtml= ' Welcome to my blog '; }); </script> of course, since the use of jquery, a simpler formulation is: <script> $(document). Ready (function () {   $ (' #ele '). HTML (' Welcome to my blog ');//This is also available. Text () method}); </script>   You can put the jquery code above anywhere on the page, and it will always wait until the page has finished loading.  
Related Article

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.