等待 iframe 在 JavaScript 中加载 [英] Wait for iframe to load in JavaScript
问题描述
我正在用 JavaScript 打开一个 iframe:
I am opening an iframe in JavaScript:
righttop.location = "timesheet_notes.php";
然后想向它传递信息:
righttop.document.notesform.ID_client.value = Client;
很明显,在页面完全加载到 iframe 之前,该行不会工作,并且要写入该表单元素.
Obviously though, that line isn't going to work until the page has fully loaded in the iframe, and that form element is there to be written to.
那么,解决这个问题的最佳/最有效方法是什么?某种超时循环?理想情况下,我真的希望将所有内容都包含在这个特定的脚本中,而不必向正在打开的页面添加任何额外的内容.
So, what is the best/most efficient way to address this? Some sort of timeout loop? Ideally I would really like to keep it all contained within this particular script, rather than having to add any extra stuff to the page that is being opened.
推荐答案
首先,我认为你应该影响 iframes 的 src
属性,而不是 location
.其次,挂钩 iframe 的 load
事件以执行您的更改:
First of all, I believe you are supposed to affect the src
property of iframes, not location
. Second of all, hook the iframe's load
event to perform your changes:
var myIframe = document.getElementById('righttop');
myIframe.addEventListener("load", function() {
this.contentWindow.document.notesform.ID_client.value = Client;
});
myIframe.src = 'timesheet_notes.php';
同样,这一切都是假设您指的是i框架,而不是框架集.
Again, this is all presuming you mean iframe, not framesets.
这篇关于等待 iframe 在 JavaScript 中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!