【问题标题】:iframe onload fires when iframe is not readyiframe 未准备好时触发 iframe onload
【发布时间】:2018-10-11 17:09:58
【问题描述】:

所以我在 vanilla React 中遇到了问题。 我在ready() 函数中有这行代码:

var current = this;    
React.createElement(
            "iframe",
            {id: "iframeController", 
             ref: "iframe", 
             src: "data:text/html;charset=utf-8," + escape(notification.description), 
             onLoad: current.iframeLoaded(this)}
)

notification.description 来自我对 this.state.notifications 中的对象所做的映射

还有这个函数跟随它

iframeLoaded(iframe) {
    if(iframe) {
        iframe.height = iframe.contentWindow.document.body.scrollHeight + "px";         
    }
}

但是,如果我 console.log iframeLoaded 中的 (iframe) 元素,它将返回为 null。如果我 getElementById 也是一样,但它是未定义的。

我做了一个 setTimeout 来查看它是否是组件更新问题,结果是。因此,即使在 Chrome 中,onLoad 也会在 src 有时间渲染之前触发。

我正在尝试获取高度,以便可以在窗口中设置它,允许它调整大小并获取我正在获取的 iframe 的完整高度。

任何人都可以就如何解决这个问题给我任何提示吗?

【问题讨论】:

    标签: javascript reactjs iframe


    【解决方案1】:

    我怀疑问题出在这一行:onLoad: current.iframeLoaded(this)}。在这种情况下,onLoad 被设置为current.iframeLoaded(this) 的返回值。

    我相信您想将值包装在函数中(不是箭头符号,而是传统函数):

    onLoad: function(){current.iframeLoaded(this);}}
    

    【讨论】:

    • 我试试看,谢谢!我想你可能是对的,我今天的大脑比平时慢了一点
    • 仍以不明叹息归来,今天的斗争是真实的呵呵
    • 实际上我使用包装函数再次尝试使用 getElementById 并且它有效。现在它只是给了我一个 Blocked frame - cross origin exception,但那很好处理不好。谢谢大佬!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多