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

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.
主站蜘蛛池模板: 林州市| 古浪县| 嘉兴市| 泗水县| 鄂伦春自治旗| 宾川县| 安阳县| 巴南区| 永年县| 乐陵市| 文山县| 福建省| 新化县| 迁安市| 盐山县| 利川市| 清远市| 慈溪市| 祁门县| 泉州市| 会昌县| 邹城市| 郸城县| 淮滨县| 鄄城县| 新乡市| 漳浦县| 克什克腾旗| 桃江县| 石棉县| 双流县| 京山县| 佛山市| 双辽市| 皮山县| 石柱| 石泉县| 公安县| 永年县| 大渡口区| 金坛市|