【发布时间】:2014-10-13 09:33:30
【问题描述】:
我正在开发一个使用 Facebook 的 React JS 框架同时支持客户端和服务器端渲染的应用程序。
我需要渲染一个 iframe,它里面有一些 html。 HTML 是使用我有权访问的脚本创建的。
但是,我希望在服务器上呈现内部内容,以便 HTML 显示在搜索引擎中。问题是要创建内部内容,通常需要等待 iframe '加载',这在服务器上不会发生。
我该怎么做?
这是我尝试过的,但不起作用:
render: function() {
return (
<div>
<iframe
ref="myIframe">
</iframe>
</div>
);
},
componentDidMount : function() {
var iFrameNode = this.refs.myIframe,
frameDoc = iFrameNode.contentWindow.document;
frameDoc.write('<html><body style="margin:0px;"><div><script type="text/javascript" src="..."></script></div> ... more html');
}
请注意,我在 componentDidMount 上添加内容,否则在 iframe 加载时它会被“擦除”。
【问题讨论】:
标签: javascript iframe reactjs