【问题标题】:javascript: waiting for an iframe page to load before writing to it (but not from the page that's triggered the load)javascript:在写入之前等待 iframe 页面加载(但不是从触发加载的页面)
【发布时间】:2009-09-23 08:31:59
【问题描述】:

抱歉,如果这已在其他地方得到回答,但我无法找到它被引用。 (我承认,可能是因为没有其他人愿意做这种愚蠢的事情)。

所以,我有一个包含三个 iframe 的页面。一个事件触发了一个 javascript 函数,该函数将新页面加载到其他两个 iframe 中; ['topright'] 和 ['bottomright']。

但是,正在加载到 iframe 'topright' 的页面中的 javascript 然后需要将信息发送到 'bottomright' iframe 中的元素。

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

但这只有在页面完全加载到右下角框架时才有效。那么,对于“topright”iframe 中的代码,在写入之前检查并确保右下角框架中的表单元素实际上可以写入,最有效的方法是什么?请记住,页面加载不是从右上角的框架触发的,所以我不能简单地使用 onLoad 函数。

(我知道这可能听起来像是从一个页面到另一个页面获取数据的极其曲折的路线,但这是另一回事。客户永远是对的,等等...:-))

【问题讨论】:

    标签: javascript iframe


    【解决方案1】:

    如果我正确理解您的问题,并且您控制了所有框架的内容,并且所有框架内容都来自同一个域,您可以在bottomright 框架上触发onload 事件以拉 来自topright 的数据,而不是试图推送它。

    bottomright 甚至可以调用topright 中的函数并要求它push 数据(在我看来这仍然是一种pull 的形式)。

    类似这样的:

    // 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');
    }
    

    未经测试。我不能 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');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多