Iframe

2018-11-07  本文已影响0人  Lethe35

1.iframe 的常用属性:

<iframe
  id="includeFrame"
  ref={ref => {
    this.approveForm = ref;
  }}
  name="iframe"
  src={formKeyV}
  width="100%"
  scrolling="auto"
  height="450px"
  frameBorder="0"
/>

2.获取iframe里面的内容

var iframe = document.getElementById("myrame"); //获取iframe标签
var iwindow = iframe.contentWindow; //获取iframe的window对象
var idoc = iwindow.document; //获取iframe的document对象
console.log(idoc.documentElement); //获取iframe的html
console.log("body",idoc.body);

// react
const iframe = this.approveForm;
const iwindow = iframe.contentWindow;
const idoc = iwindow.document; // 与this.approveForm.contentDocument一致
dom.contentWindow.postMessage(JSON.stringify(data), '*'); // 后面继续postMessage

3.iframe 的优缺点

4.拓展

window.addEventListener("message", receiveMessage, false);

function receiveMessage(event)
{
  // For Chrome, the origin property is in the event.originalEvent
  // object. 
  // 这里不准确,chrome没有这个属性
  // var origin = event.origin || event.originalEvent.origin; 
  var origin = event.origin
  if (origin !== "http://example.org:8080")
    return;

  // ...
}
  • message 的属性有:
    data:
    从其他 window 中传递过来的对象。
    origin:
    调用 postMessage 时消息发送方窗口的 origin . 这个字符串由 协议、“://“、域名、“ : 端口号”拼接而成。例如 “https://example.org (隐含端口 443)”、“http://example.net (隐含端口 80)”、“http://example.com:8080”。请注意,这个origin不能保证是该窗口的当前或未来origin,因为postMessage被调用后可能被导航到不同的位置。
    source:
    对发送消息的窗口对象的引用; 您可以使用此来在具有不同origin的两个窗口之间建立双向通信。

iframe高度自适应的6个方法

上一篇 下一篇

猜你喜欢

热点阅读