如何在head标签的顶部/开头插入脚本标记? [英] How do I insert a script tag in to the top/beginning of head tag?

查看:276
本文介绍了如何在head标签的顶部/开头插入脚本标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在head标记中的所有其余脚本之前插入脚本标记。如何使用原生javascript进行此操作?

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>

当我使用它时,它只会附加在head标签中的所有标签之后。

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

document.getElementsByTagName("head")[0].appendChild(script);


推荐答案

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

head.insertBefore(script, head.firstChild);

这篇关于如何在head标签的顶部/开头插入脚本标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆