The first thing to do is put your functions in a ready-made method that will perform these functions after loading the DOM.
<script type="text/javascript"> jQuery(document).ready(function() { </script>
Then just put the script in front of </body>. Thus, append will be launched in the same way as dom.
<script type="text/javascript"> $('head').append(); </script>
Another approach is to use a load event:
Check this link with a working example, inside it I set the body with a green background, it loads the stackoverflow stylesheet, which when the download is completed changes the body to white.
http://jsfiddle.net/CQbqA/1/
source share