In my document I have an onLoad function given like here in this example:
<body onLoad="display()">
In addition I added a function at the end of the document which changes some CSS properties:
<script>
window.onload = foo(), bar();
</script>
Somehow the whole thing doesn't work! I tried to add all functions at the end of the document but I don't get it, somehow they don't trigger!
That will invoke the functions, but it doesn't assign anything useful to
window.onload
unless your last function happens to return a function.You need to assign a function to
window.onload
that will invoke your functions when thewindow
is ready.Plus, since you're putting the script at the bottom, you probably don't need the
window.onload
.You should also be aware that assigning directly to
window.onload
will overwrite the script assigned in the<body onLoad=...>
, so you shouldn't do both. Currently, the return value ofbar();
is wiping out the function that invokesdisplay();
.Getting rid of the
window.onload
assignment will fix that.In the first case, I don't see a reason why it shouldn't work, as long as
display()
is defined function with no errors.In the second case, when assigning event handlers via DOM, you need to pass either a function reference (i.e. instead of
foo()
justfoo
) or wrap it in an anonymous function like so: