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

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

问题描述

我想在 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>

当我使用它时,它只是附加在头标签中的所有标签之后.

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);

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

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