我有可以将 iframe 插入网页的浏览器插件。尽可能早地插入 iframe(使用 DOMContentLoaded
),以便 iframe 的内容随页面呈现。
但是在 Firefox 中不会发生这种情况。尽管 iframe 似乎提前插入,但页面/图像/等的内容先于 iframe 的内容加载。
是否有解决此 Firefox 行为的方法,以便可以提前或首先加载 iframe 内容?
不确定这是否重要,但这里有两种方法具有相同的结果:
方法 1. 在 DOMContentLoaded
上插入 iframe
方法二:
nsiWebProgressListener()
onStateChange: function (aBrowser, aWebProgress, aRequest, aStateFlags, aStatus) {
const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START;
const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP;
if (aStateFlags & STATE_STOP) { //document is undefined at STATE_START
try {
//create iframe
} catch (e) {}
}
return true;
},
最佳答案
监听器 DOMContentLoaded
触发的时间比人们在 FF 上想象的要晚。通过使用 Firefox 的 onLocationChange
(在 URL 更改时触发)并将 setTimeout 循环设置为 50 毫秒解决了这个问题。
关于javascript - 插入 iFrame,然后在 FF 上的所有其他内容之前加载它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4488293/