官术网_书友最值得收藏!

Timing JavaScript execution

One important point to note while dealing with JavaScript execution timing is to be sure that the entire page has finished loading prior to allowing the JavaScript to begin executing its code. The reason for waiting for page to load is to allow all of the assets and external references to load on the page before attempting to manipulate them. If your JavaScript attempts to perform an action on an element that doesn't exist, your application flow could fail. To avoid this issue, we can add an event listener to the DOM allowing it to run only once the page has been completely loaded and comes to display. Utilizing the DOM event for JavaScript offers an easy way to do just this as shown in the following code:

window.addEventListener("load", init, false);

var init = function() {
  // Start everything from in here.
}

Now, when the window has completed its load process the init function is called and the remainder of the application code can begin execution. JavaScript actually has a number of ways to accomplish execution of the code, specifically once the page has finished loading. The following chapters of this book will use and explain many of them using examples.

主站蜘蛛池模板: 含山县| 广河县| 武山县| 锡林郭勒盟| 清镇市| 晋宁县| 太白县| 白银市| 临武县| 江华| 嵊泗县| 车险| 临湘市| 白沙| 清徐县| 襄城县| 靖宇县| 宣化县| 长垣县| 安国市| 滁州市| 定陶县| 乐清市| 钦州市| 锡林浩特市| 嵊泗县| 浠水县| 丹凤县| 鄂伦春自治旗| 嘉祥县| 内黄县| 南康市| 金华市| 吉林市| 云梦县| 赣榆县| 吴川市| 泗洪县| 东辽县| 武夷山市| 海城市|