1.iframe 的常用属性:
- name : 名称。
- width: 宽度。
- height :高度。
- marginheight:定义 iframe 的顶部和底部的边距
- marginwidth:定义 iframe 的左侧和右侧的边距
- src :规定在 <iframe> 中显示的文档的 URL。
- frameborder : 规定是否显示 <iframe> 周围的边框。 (0为无边框,1位有边框)。
- align : 规定如何根据周围的元素来对齐 <iframe>。不赞成使用。请使用样式代替。(left,right,top,middle,bottom)。
- scrolling : 规定是否在 <iframe> 中显示滚动条。 (yes,no,auto)
<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 的优缺点
-
优点:
重载页面时不需要重载整个页面,只需要重载页面中的一个框架页(减少数据的传输,减少网页的加载时间);
技术简单,使用方便,主要应用于不需要搜索引擎来搜索的页面;
方便开发,减少代码的重复率(比如页面的header,footer); -
缺点:
会产生很多的页面,不易于管理;
不易打印;
多框架的页面会增加服务气得http请求;
浏览器的后退按钮无效等;
4.拓展
window.postMessage()
window.postMessage() 方法可以安全地实现跨源通信。
通常,对于两个不同页面的脚本,只有当执行它们的页面位于具有相同的协议(通常为https),端口号(443为https的默认值),以及主机 (两个页面的模数Document.domain
设置为相同的值) 时,这两个脚本才能相互通信。window.postMessage() 方法提供了一种受控机制来规避此限制,只要正确的使用,这种方法就很安全。
window.postMessage()方法被调用时,会在所有页面脚本执行完毕之后(e.g., 在该方法之后设置的事件、之前设置的timeout 事件,etc.)向目标窗口派发一个MessageEvent
消息。 该MessageEvent
消息有四个属性需要注意: message 属性表示该message 的类型; data 属性为 window.postMessage 的第一个参数;origin 属性表示调用window.postMessage() 方法时调用页面的当前状态; source 属性记录调用 window.postMessage() 方法的窗口信息语法
otherWindow.postMessage(message, targetOrigin, [transfer]);
otherWindow
其他窗口的一个引用,比如iframe的contentWindow属性、执行window.open返回的窗口对象、或者是命名过或数值索引的window.frames。
message
将要发送到其他 window的数据。它将会被结构化克隆算法序列化。这意味着你可以不受什么限制的将数据对象安全的传送给目标窗口而无需自己序列化。[1]
targetOrigin
通过窗口的origin属性来指定哪些窗口能接收到消息事件,其值可以是字符串 “ * ” (表示无限制)或者一个URI,在发送消息的时候,如果目标窗口的协议、主机地址或端口这三者的任意一项不匹配targetOrigin提供的值,那么消息就不会被发送;只有三者完全匹配,消息才会被发送。这个机制用来控制消息可以发送到哪些窗口;例如,当用postMessage传送密码时,这个参数就显得尤为重要,必须保证它的值与这条包含密码的信息的预期接受者的origin属性完全一致,来防止密码被恶意的第三方截获。如果你明确的知道消息应该发送到哪个窗口,那么请始终提供一个有确切值的targetOrigin,而不是 * 。不提供确切的目标将导致数据泄露到任何对数据感兴趣的恶意站点。
transfer
可选</dt>
是一串和message 同时传递的Transferable
对象. 这些对象的所有权将被转移给消息的接收方,而发送一方将不再保有所有权。执行如下代码, 其他window可以监听派遣的message:
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的两个窗口之间建立双向通信。