dojo.addOnLoad

dojo.addOnLoad(Function fn)

Sooner or later,every Javascript programmer tries something like this:

<script>
if (dayOfWeek == "Sunday" ){
document. musicPrefs. other. value = "Afrobeat";
}
</script>
<form name= "musicPrefs">
<input type= "text" name= "other">
...

It doesn't work because the "other" control is not defined yet. You can move the code to the bottom of the page,but that removes the linear nature of HTML. If you're reading the code,you want to zero in on a control,and see the code that influences it close by.

dojo.addOnLoad(...) defers script execution until all the HTML is loaded. So this code:

function setAfrobeat (){
document. musicPrefs. other. value= "Afrobeat";
}
dojo. addOnLoad (setAfrobeat );

conveniently replaces the one above. When the function is small,you may prefer to write it inline:

dojo. addOnLoad (
function (){
document. musicPrefs. other. value= "Afrobeat";
}
)
;

This is the function literal or anonymous function construct of JavaScript. If it looks really,really wierd to you,take a peek ahead at Functions as Variables for an explanation.

相关文章

我有一个网格,可以根据更大的树结构编辑小块数据.为了更容易...
我即将开始开发一款教育性的视频游戏.我已经决定以一种我可以...
我正在使用带有Grails2.3.9的Dojo1.9.DojoNumberTextBox小部...
1.引言鉴于个人需求的转变,本系列将记录自学arcgisapiforja...
我正在阅读使用dojo’sdeclare进行类创建的语法.描述令人困惑...
我的团队由更多的java人员和JavaScript经验丰富组成.我知道这...