support for inserting the script into head

added document.ready check