惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
IT之家
IT之家
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
U
Unit 42
爱范儿
爱范儿
博客园 - 聂微东
F
Fortinet All Blogs
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
L
LangChain Blog
雷峰网
雷峰网
B
Blog RSS Feed
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Engineering at Meta
Engineering at Meta
H
Hackread – Cybersecurity News, Data Breaches, AI and More

月光博客

AI编程从零开始:环境配置到开发调试-月光博客 母亲被保健品诈骗-月光博客 向身体低头,向岁月妥协:我的高血压“还债日记” -月光博客 月光博客电子书:《信息安全指南》 谷歌发布2025年度搜索排行榜 中国2025社会热点大事记 2025年十大流行语发布 7天3次,骗子骗走我母亲95万元 “技术男”设三重安全墙,母亲95万存款还是被骗走了 电信诈骗后的复盘:母亲的95万元,是怎么从手机银行里消失的 母亲被电信诈骗95万元的全过程 阿根廷警察被谷歌街景相机拍到裸照 网信办开展“清朗·整治恶意挑动负面情绪问题”专项行动 翟欣欣敲诈勒索案一审获刑12年 《绝命毒师》影评:力工觉醒后的梭哈至死 《人工智能生成合成内容标识办法》正式施行 用AI分析你的财务信息 腾讯子公司实习HR怒怼求职者后被开除 OpenAI发布最强模型GPT-5,免费向所有用户开放 用AI解构你的日记 用AI分析你的游戏偏好 用AI分析你的观影偏好 用AI分析你的听歌偏好 《白鹿原》人物分析:乱世浮沉中的人性剖析 乌托邦的捷径:让AI治理“失败国家” 苹果AI的“路径错误”:为什么它在大模型时代掉队了? 《暗黑破坏神3》第35赛季开荒指南 我对特朗普的看法 欧·亨利十大经典小说鉴赏 HBO电视剧《最后生还者》第二季影评
在JS文件中加载JS文件的方法
月光 · 2021-10-23 · via 月光博客

在很多情况下,我们会遇到一个问题,就是如何在一个Javascript文件里,再加载另一个Javascript文件,并完成一定的功能,如何实现一个JS文件加载另一个JS文件呢?

有些人使用document.write的方式来加载js,这种方法有很多问题,并不推荐使用这种方法。

这里就介绍几种常见的调用方法。

先创建一个公共的脚本文件如下:

var js = document.createElement('script');
js.src = 'myscript.js';

接着通过几种不同的方法将该脚本加载。

1、加载在头部

var js = document.createElement('script');
js.src = 'myscript.js';
document.getElementsByTagName('head')[0].appendChild(js);

另一种写法是:

var js = document.createElement('script');
js.src = 'myscript.js';
document.head.appendChild(js);

2、加载在BODY中

加载在页面中的写法如下:

var js = document.createElement('script');
js.src = 'myscript.js';
document.body.appendChild(js);

这种加载方法存在一个问题,就是有可能代码是在head区域,导致body还没达到,document.body就不存在,代码就会出错。

3、使用documentElement

document.documentElement就是html文档本身,因此肯定是存在的,这种调用的写法如下:

var js = document.createElement('script');
js.src = 'myscript.js';
var html = document.documentElement;
html.insertBefore(js, html.firstChild);

4、加载在第一个脚本前

这种方法是把js文件插入到第一个出现script的标识前,除非网页里没有任何一个script出现,否则应该不会出错。代码的写法如下:

var js = document.createElement('script');
js.src = 'myscript.js';
var first = document.getElementsByTagName('script')[0];
first.parentNode.insertBefore(js, first);

5、加载在当前JS文件之前或之后

这种方法是把js文件插入到目前所在的js文件前,代码的写法如下:

var js = document.createElement('script');
js.src = 'myscript.js';
var first  = document.getElementsByTagName('script'); 
var here = first[first.length-1]; 
here.parentNode.insertBefore(js,here); 

加载在当前js文件之后,代码的写法如下:

var js = document.createElement('script');
js.src = 'myscript.js';
var first  = document.getElementsByTagName('script'); 
var here = first[first.length-1]; 
here.parentNode.appendChild(js);

附录:1、在JS文件里加载CSS文件

var link = document.createElement('link');
link.setAttribute('type', 'text/css');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('href', 'mycss.css');
document.head.appendChild(link);

附录:2、在JS文件里设置META

var meta = document.createElement('meta');
meta.setAttribute('name','viewport');
meta.setAttribute('content','width=device-width, initial-scale=1');
document.head.appendChild(meta);

在JS文件中加载JS文件的方法