Home > Web Front-end > JS Tutorial > body text

A brief introduction to JavaScript execution order

黄舟
Release: 2017-03-14 15:12:43
Original
1335 people have browsed it

This is an article about the basics of JavaScript. It mainly explains the order in which JavaScript is executed after the web page is loaded. This is very important for us to understand the running mechanism of JavaScript. Let’s take a look. .

Javascript is executed in a top-down order. Unless you specify otherwise, the Javascript code will not wait until the page is loaded before executing. For example, a web page contains the following HTML code:

welcome to www.codeceo.com

Copy after login

If you add the following Javascript code before this line of HTML code:

Copy after login

When you run the page, you will get an error like this Message : "document.getElementById('ele') is null." The reason is that when the above javascript is run, there is no DOM element with the ID 'ele' on the page yet.

There are two solutions:

1. Place the javascript code after the HTML code:

welcome to www.codeceo.com

Copy after login

2. Wait until the web page is loaded and then run the javascript code. You can use the traditional solution (load): first add the HTML body and add "," and then call the above javascript code in load()function. What I want to focus on here is to use jQuery to achieve it:


//当然,既然用到了jQuery,更简单的写法是:
Copy after login

You can place the above jQuery code anywhere on the page, and it will always wait until the page is loaded before executing.

The above is the detailed content of A brief introduction to JavaScript execution order. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!