简体   繁体   English

javascript:在写入iframe页面之前,先等待它加载(但不是从触发加载的页面开始)

[英]javascript: waiting for an iframe page to load before writing to it (but not from the page that's triggered the load)

Apologies if this has been answered elsewhere, but I haven't been able to find it referenced. 抱歉,如果在其他地方也回答过此问题,但我无法找到它的引用。 (Probably because nobody else would want to do such a daft thing, I admit). (我承认,可能是因为没人愿意做这种愚蠢的事情)。

So, I have a page with three iframes in it. 因此,我有一个包含三个iframe的页面。 An event on one triggers a javascript function which loads new pages into the other two iframes; 一个事件触发javascript函数,将新页面加载到其他两个iframe中; ['topright'] and ['bottomright']. ['topright']和['bottomright']。

However, javascript in the page that is being loaded into iframe 'topright' then needs to send information to elements in the 'bottomright' iframe. 但是,正在加载到iframe“顶部”的页面中的javascript然后需要将信息发送到“底部” iframe中的元素。

window.frames['bottomright'].document.subform.ID_client = client; window.frames ['bottomright']。document.subform.ID_client = client; etc 等等

But this will only work if the page has fully loaded into the bottomright frame. 但这仅在页面已完全加载到右下框架中时有效。 So what would be the most efficient way for that code in the 'topright' iframe to check and ensure that that form element in the bottomright frame is actually available to write to, before it does write to it? 那么,在“右上” iframe中的代码在写入之前,检查并确保右下框中的form元素实际上可用于写入的最有效方法是什么? Bearing in mind that the page load has NOT been triggered from the topright frame, so I can't simply use an onLoad function. 请记住,页面加载尚未从右上框架触发,因此我不能简单地使用onLoad函数。

(I know this probably sounds like a hideously tortuous route for getting data from one page to another, but that's another story. The client is always right, etc...:-)) (我知道这听起来像是一条曲折的曲折路线,用于将数据从一页转移到另一页,但这是另一回事了。客户端总是正确的,等等... :-)

If I understand your problem correctly, and you control the content of all the frames, and all the frame content is from the same domain, you can trigger an onload event on the bottomright frame to pull the data from topright , rather than trying to push it. 如果我没有理解你的问题,你控制所有的帧的内容,所有的帧内容是来自同一个域,你可以触发一个onload的事件bottomright框架来自于该数据topright ,而不是试图推动它。

bottomright can even invoke a function in topright and ask it to push the data (which in my mind is still a form of pull ). bottomright甚至可以调用一个函数topright ,并要求它数据(这在我的脑海里仍然是的一种形式)。

Something like this: 像这样:

// topright
function pushData(frame, form, name, value) {
    frame.document.forms[form].elements[name] = value;
}

// bottomright
window.onload = function () {
    top.frames['topright'].pushData(window, 'sub_form', 'ID_client', 'client');
}

Untested. 未经测试。 I'm not 100% sure passing window from bottomright will work, in which case this might be an alternative: 我不是100%确定从bottomright通过window是可以的,在这种情况下,这可能是替代方案:

// topright
function pushData(frame, form, name, value) {
    top.frames[frame].document.forms[form].elements[name] = value;
}

// bottomright
window.onload = function () {
    top.frames['topright'].pushData('bottomright', 'sub_form', 'ID_client', 'client');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM