我动态创建了一个 iframe 并添加了src
归因于它。然后我将此 iframe 附加到页面正文中。知道我想附上onload
向 iframe 发送事件以读取 iframe 内容。有人可以建议我该怎么做吗?
frame = document.createElement('iframe');
frame.setAttribute('src','http://example.com');
body.appendChild(frame);
frame.onload = function(){
alert('hi'); // here I want to read the content in the frame.
}
有些浏览器确实有 iframe 的 onload 事件,您应该首先尝试附加它,然后再设置 iframe 的 src 属性。
我会避免完全使用它,因为在某些浏览器中它可能不会在某些条件下触发(例如目标位于 IE 的缓存中)。
您可以使用计时器来检查框架的 contentWindow 的就绪状态是否完成
var inter = window.setInterval(function() {
if (frame.contentWindow.document.readyState === "complete") {
window.clearInterval(inter);
// grab the content of the iframe here
}
}, 100);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)