我有一个投资组合页面,其中嵌入了大约 12 个 iframe 元素。我想知道是否有一种方法可以缓存或自动加载 iframe 元素,这样我就不需要在每次加载网站时都重新加载。
这是我的 iframe 元素的样子:
<iframe src="http://iframe.url" width="300" height="600" frameborder="0"></iframe>
iframe 元素包含横幅广告,由图像、HTML 和 JS 代码组成。请让我知道如何完成这项任务。感谢您的帮助!
最佳答案
我在这里引用了这些信息。如果我明白你想要什么,这看起来很有希望。
Friendly Iframe
This technique is for ads. It's not really an iframe loading technique, but more of a way to use an iframe to hold ads. The magic is not in how the iframe is loaded, but in how the main page, the iframe and ad codes work together. It works like this:
创建一个 iframe。将源设置为来自同一域的小型静态 html 页面
在 iframe 内,设置 JavaScript 变量 inDapIF = true 以告知广告它是在友好的 iframe 内加载的
在 iframe 中,创建一个以广告 url 作为源的脚本元素。像普通的 JavaScript 广告一样加载广告
广告加载完毕后,调整 iframe 的大小以适应广告的尺寸。
友好的 iframe 适用于所有浏览器。
列出了其他技术,但这似乎适合您的用例。在 Iframe loading techniques and performance 查看其他方法
如果您想要一种强制 iFrame 重新加载的方法,这里有一个 SO 答案。 How to force an iFrame to reload once it loads
关于javascript - 如何自动加载 iframe 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42077366/