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

Making sure the DOM is available

When a web page loads, all the HTML elements that comprise the page are loaded and interpreted. This is known as the DOM (Document Object Model). Your JavaScript must not attempt to access any of these elements until all the elements have loaded. Obviously if your JavaScript code attempts to access an element that hasn't been loaded it will cause an error. To control this, Dojo has a ready() function that you can include inside the require() function, which will execute only after all the HTML elements and any modules have loaded. Alternatively, you can use the dojo/domReady! plugin to ensure that all the HTML elements have been loaded. We'll use the second method here:

<script>     
require(["esri/map", "dojo/domReady!"], function(Map) { 
         
    }); 
</script> 

Note that, even though we have included the dojo/domReady! module in the list of modules we want to load, we have not provided an alias for it. This is because although we need the plugin to inform the application that the DOM is ready, we don't need to refer to it explicitly in our code. This is the exception to the rule we mentioned earlier.

Although it is certainly possible to add JavaScript code directly inside your HTML page, it is better practice to create a separate JavaScript file ( .js). Most of the code that we write in this book will be done inside an HTML file for simplicity, but as your applications become more complex you'll want to adhere to the practice of writing your JavaScript code in a separate file or files.
主站蜘蛛池模板: 龙泉市| 宜春市| 江都市| 电白县| 于都县| 温泉县| 通江县| 迁安市| 明光市| 青海省| 金山区| 渝中区| 汕尾市| 盐山县| 南丰县| 会泽县| 霍城县| 东乌珠穆沁旗| 洛南县| 越西县| 北川| 江城| 泊头市| 宁化县| 兰考县| 徐闻县| 平乐县| 内江市| 原平市| 平湖市| 响水县| 萝北县| 福安市| 沭阳县| 南充市| 建始县| 开封县| 西丰县| 贵南县| 黔江区| 龙南县|