在 HEAD 中最后一个硬编码 JavaScript 文件引用之后附加 JavaScript 文件引用
如果以下内容听起来有点奇怪,我们深表歉意。我在旧框架中工作,该框架不允许访问核心模板,因此无法更改文件的加载顺序。
我想做的是在所述文档 HEAD 中的最终脚本之后在文档的 HEAD 中加载 JS 文件。
我目前正在使用以下内容,但没有成功:
var head = document.getElementsByTagName("head")[0];
var headScripts = head.getElementsByTagName("script");
var headScriptsLength = headScripts.length - 1;
var headScripts = headScripts[headScriptsLength];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = '/Global/ICIS/Scripts/global.js';
headScripts.appendChild(newScript);
它将对 global.js 的引用直接添加到创建它的块之后。
Apologies if the following sounds a little strange. Am working in a legacy framework that allows no access to core templates so cannot alter the loading order of files.
What i am trying to do is load a JS file in the HEAD of the document after the final SCRIPt in said document HEAD.
I currently am using the following with no success:
var head = document.getElementsByTagName("head")[0];
var headScripts = head.getElementsByTagName("script");
var headScriptsLength = headScripts.length - 1;
var headScripts = headScripts[headScriptsLength];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = '/Global/ICIS/Scripts/global.js';
headScripts.appendChild(newScript);
It adds the reference to global.js directly after the block that creates it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您最好将脚本简单地附加到头部,这样它将成为您的最后一个脚本。
You better off appending your script simply to the head, and so it will become your last script.