Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert a script tag in to the top/beginning of head tag?

I want to insert a script tag before all the rest of the scripts in the head tag. How would I do that with native javascript?

<head>
    //INSERT SCRIPT HERE
     <script type="text/javascript" src="common.js"></script>
     <script type="text/javascript" src="omni-controls.js"></script>
</head>

When I use this, it just appends after all the tags in the head tag.

document.getElementsByTagName("head")[0].appendChild(script);
like image 888
Justin Avatar asked Oct 19 '12 17:10

Justin


1 Answers

var head = document.getElementsByTagName("head")[0]

head.insertBefore(script, head.firstChild);
like image 104
I Hate Lazy Avatar answered Sep 18 '22 07:09

I Hate Lazy