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

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.
主站蜘蛛池模板: 达尔| 峡江县| 平南县| 托克逊县| 阳高县| 淮北市| 南漳县| 山东省| 蛟河市| 永善县| 雅安市| 农安县| 松潘县| 阆中市| 普宁市| 沈阳市| 扬州市| 沂水县| 同心县| 霍山县| 甘孜县| 延长县| 呼和浩特市| 绥棱县| 中宁县| 张掖市| 铁力市| 梧州市| 榆社县| 哈尔滨市| 甘泉县| 榆林市| 电白县| 赫章县| 澄迈县| 沙洋县| 建瓯市| 突泉县| 镇安县| 莎车县| 嘉鱼县|