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

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.

主站蜘蛛池模板: 新丰县| 永善县| 宜丰县| 三江| 木里| 吴桥县| 镇原县| 盐山县| 财经| 晋中市| 凤庆县| 苍南县| 志丹县| 水富县| 南华县| 茌平县| 福州市| 大英县| 广德县| 会泽县| 宁德市| 钟山县| 台北市| 正安县| 新建县| 新安县| 永善县| 湖北省| 尖扎县| 尚志市| 富蕴县| 东乌| 灵丘县| 榆社县| 伊金霍洛旗| 酉阳| 交城县| 城市| 化州市| 淳安县| 建水县|