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

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.

主站蜘蛛池模板: 松滋市| 图片| 荣成市| 曲松县| 北宁市| 揭西县| 伊宁市| 忻州市| 无棣县| 西充县| 天水市| 彭州市| 英德市| 三都| 南皮县| 股票| 前郭尔| 西贡区| 祥云县| 丰都县| 胶州市| 桦甸市| 岗巴县| 苍山县| 乌苏市| 微山县| 阿克陶县| 平度市| 旌德县| 灵丘县| 郴州市| 靖远县| 蕲春县| 靖西县| 墨脱县| 恭城| 婺源县| 崇信县| 灵石县| 乐安县| 闽侯县|