如何使用 js 以编程方式使用 iframe 的 onload?
我有一个用js创建的iframe,这是我到目前为止的代码:
<script>
function loadIframe(url) {
ifr = document.createElement("IFRAME");
ifr.setAttribute("src", url);
ifr.style.width = "800px";
ifr.style.height = "600px";
document.body.appendChild(ifr);
ifr.onload = ready();
}
function ready() { alert("ready") };
window.onload = function () {
loadIframe("http://www.wallpaperpimper.com/wallpaper/Landscape/Ocean/Big-wave-1-S79KZQKBWP-1600x1200.jpg");
}
</script>
iframe被创建,但ready()函数在iframe加载内容之前触发,我基本上想做的是:
<iframe onload="ready()" src="http://www.wallpaperpimper.com/wallpaper/Landscape/Ocean/Big-wave-1-S79KZQKBWP-1600x1200.jpg"></iframe>
工作正常,准备好一旦 iframe 内容加载,() 函数就会触发,但我想从 js 中完成这一切。
我无法在加载到 iframe 的页面的正文中使用 onload,因为我不拥有它们。
我应该如何设置onload事件?
I have this iframe I create with js, this is the code I have so far:
<script>
function loadIframe(url) {
ifr = document.createElement("IFRAME");
ifr.setAttribute("src", url);
ifr.style.width = "800px";
ifr.style.height = "600px";
document.body.appendChild(ifr);
ifr.onload = ready();
}
function ready() { alert("ready") };
window.onload = function () {
loadIframe("http://www.wallpaperpimper.com/wallpaper/Landscape/Ocean/Big-wave-1-S79KZQKBWP-1600x1200.jpg");
}
</script>
The iframe gets created but the ready() funcion fires before the iframe loads the content, what I basically wants to do is:
<iframe onload="ready()" src="http://www.wallpaperpimper.com/wallpaper/Landscape/Ocean/Big-wave-1-S79KZQKBWP-1600x1200.jpg"></iframe>
That works fine, the ready() function fires once the iframe content is loaded, but I want to do it all from js.
I can't use onload inside the body on the pages loaded into the iframe because I don't own them.
How should I set the onload event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过这样做:
您正在执行ready函数并将结果分配给irf.onload,而不是将对ready函数的引用分配给onload属性。所以我认为你的意思是:
因此将对就绪函数的引用分配给 onload 属性而不是执行它。
By doing this:
You're executing the ready function and assigning the results to irf.onload instead of assigning a reference to the ready function to the onload property. So I think you meant to do:
So assigning a reference to the ready-function to the onload property instead of executing it.