If you can control where the script tag is placed, put your JavaScript just before the closing </body>
tag:
<script>
foo();
</script>
Since the DOM will already be loaded by then, your function runs immediately without extra event listeners.
Hope this was helpful