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

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.

主站蜘蛛池模板: 鹤岗市| 湘潭市| 隆德县| 涿鹿县| 万宁市| 繁昌县| 饶阳县| 原平市| 德阳市| 朔州市| 邵东县| 闻喜县| 怀安县| 东丽区| 合肥市| 赤水市| 六枝特区| 兰坪| 常山县| 乌拉特前旗| 襄樊市| 新闻| 亚东县| 汉中市| 佛山市| 长海县| 晋州市| 乌审旗| 延边| 永兴县| 沙坪坝区| 德化县| 济宁市| 绥化市| 成武县| 汕尾市| 巴中市| 维西| 蒙自县| 若尔盖县| 吐鲁番市|