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

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.
主站蜘蛛池模板: 晋江市| 贵定县| 古蔺县| 民县| 淮南市| 七台河市| 扶绥县| 遵义市| 呼图壁县| 双牌县| 五寨县| 平度市| 射阳县| 湘潭市| 广南县| 铜梁县| 镇康县| 江源县| 弥渡县| 拜泉县| 敦煌市| 明光市| 霞浦县| 盐边县| 错那县| 松江区| 安岳县| 唐河县| 澎湖县| 分宜县| 安达市| 亚东县| 东乡县| 东兴市| 大同市| 鹿泉市| 沙雅县| 房山区| 布尔津县| 黄陵县| 永兴县|