Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

动态添加具有src和没有src的script标签时,能否保证执行顺序?

动态添加两个<script>标签,一个有src属性,一个没有src属性,如果想有src的脚本先执行,应该怎么写呢?如果有多个src文件需要先执行呢?

//引入的js文件
console.log('import');
<script src="...js"></script>
<script>console.log('script');</script>

输出:

script
import

不带src用innerHTML填入及哦啊本内容的<script>标签似乎在插入后立刻就执行了,而带有src的标签则需要等待请求,不知道应该如何解决这个问题?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

前一个 onload 事件完再动态增加第二个。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...