gpt4 book ai didi

javascript - 同步加载 iframe 和脚本

转载 作者:行者123 更新时间:2023-11-28 03:54:10 24 4
gpt4 key购买 nike

我有<iframe src="some source"><script src="source.js"></script>

我要加载iframe之前 script .

在 html 中简单的连续插入不起作用(有时脚本加载早于 iframe)。

另外,捕获 iframe.onload动态添加脚本不是我想要的,因为 source.js包含 document.write并且 chrome 会阻止异步脚本。

最佳答案

defer 属性添加到您的脚本中,以便它在页面加载后才加载:

<iframe src="some source"> 
<script defer src="source.js"></script>

更新: 对于不知道为什么 defer 不能使用包含 document.write() 的脚本的任何人(比如我),这是来自 MDN 的解释:

Note: document.write in deferred or asynchronous scripts will be ignored, and you'll get a message like "A call to document.write() from an asynchronously-loaded external script was ignored" in the error console.

关于javascript - 同步加载 iframe 和脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43937177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com