I don't know why I have an function like:
<script type="text/javascript">
func开发者_Go百科tion setSomeEnvValue() {
myLoadStatus = true;
myColor = 'black';
}
window.onload = setSomeEnvValue();
</script>
I don't know why IE show error at:
window.onload = setSomeEnvValue();
When I change this code to:
window.onload = setSomeEnvValue;
It makes me crazy. If setSomeEnvValue()
has some arguments like setSomeEnvValue(myVar1, myVar2)
, how can I call it when using it in IE?
Thanks you very much!
In this case, IE has the correct implementation.
When you set a callback (or event handler), such as window.onload
, you're basically telling the browser that the function that you're giving it should be executed when that event is triggered.
By writing window.onload = setSomeEnvValue();
, you're saying that the return value of setSomeEnvValue should be called whenever the window.onload
event is triggered. Of course, this may be possible if your function returns a closure, but I'm going to assume that's not the case here.
What window.onload = setSomeEnvValue();
means is to run setSomeEnvValue()
now and assign its result to window.onload
. What you need is to assign the function itself to window.onload
, so that the browser can run it later.
If you want window.onload
to call a function with some arguments that you know in advance, make a new function (with no arguments) that calls the other function with the desired arguments, and assign that to window.onload
.
function myOnload() {
setSomeEnvValue(myVar1, myVar2);
}
window.onload = myOnload;
or even
window.onload = function() { setSomeEnvValue(myVar1, myVar2); }
精彩评论