It can be easier than that. On the home page of http://www.propertyinvestor.co.nz I use php to generate the names of 2 graphics and I then use javascript to preload them. Check the bottom of the body section.
If you have
<script>
alert("hello world")
</script>
on your page you don't need to do anything to run it, the browser will just do the alert when it gets to that part of the page because it's not in a function.
alternatively I could have had linked a .js file in my head section and then in my code called
<script>
preloadthisfile('<?=$filename?>');
</script>
Therefore you could use php to generate javascript which runs automatically. Create some test scripts, play with it. It's fun working serverside and clientside together to get the funcationality you want.