There is a forum section further down called "clientside technologies" which would be appropriate for anything not directly related to PHP, such as this question.
To call a function, you can set an onload event handler for the body element
<body onload="foo();">
If you do not require the body to finish loading first, you could also call a function by having javascript code execute directly while loading the document. This is common for showing flash content, since javascript can be used to determin if the user can see flash content and otherwise show a static image or nothing at all, whereas the noscript way of doing this will show an ugly square saying something like "missing plugin".
And the reason this code is often found within the middle of the body element is that they tend to make use of document.write(...). Still, it is entirely possible to do these things after the body has finished loading as well by using other things than document.write.
<body>
<!-- more html code here ... -->
<!-- optionally followup the script element with a noscript element for browsers
with no js support or where user has disabled js -->
<script type="text/javascript">showFlashMovieIfFlashSupportPresent();</script>
<noscript><object ...><params /><embed... /></object></noscript>
<!-- more html code here ... -->
</body>