iframe 详解、案例

简单使用

我们通常使用iframe最基本的特性,就是能自由操作iframe和父框架的内容(DOM). 但前提条件是同域. 如果跨域顶多只能实现页面跳转

<iframe src="./iframeA.html"></iframe>  同域
or
<iframe src="https://cn.bing.com/"></iframe> 跨域

iframeA.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body style="height:500px">
  <div id="iframeAID">我是iframeA</div>
  <input type="text" id="date" value="2022-10-24"/>
</body>
</html>

iframe常用属性

name:框架的名称,window.frames[name]时专用的属性。
src:内框架的地址,可以使页面地址,也可以是图片的地址。
width:框架作为一个普通元素的宽度,建议使用css设置。
height:框架作为一个普通元素的高度,建议在使用css设置。
scrolling:框架的是否滚动。yes,no,auto。
frameborder:是否显示边框,1(yes),0(no)
srcdoc , 用来替代原来HTML body里面的内容。但是IE不支持, 不过也没什么卵用
sandbox: 对iframe进行一些列限制,IE10+支持

获取iframe里的内容

主要的两个API就是contentWindow,和contentDocument,这两个API只是DOM节点提供的方式(即getELement系列对象)

// iframe.contentWindow, 获取iframe的window对象
// iframe.contentDocument, 获取iframe的document对象
<script>
    window.onload = function(){
    let obj= document.getElementById("iframe1");
    // 获取iframe的window对象
    let objWindow = obj.contentWindow
    console.log(objWindow); 
    console.log(objWindow.document); // 获取iframe的window对象的document
    console.log(objWindow.document.documentElement); // 获取html
    console.log(objWindow.document.head); // 获取head
    console.log(objWindow.document.body); // 获取body
    // 获取iframe的document
    let objContentDocument = obj.contentDocument
    console.log(objContentDocument);  
     // 结合Name属性,通过window提供的frames获取.
    console.log(window.frames['iframe1'].window);
    
    // 修改子页面样式
    let obj= document.getElementById("iframe1");
    let box = obj.contentWindow.document.getElementById("iframeAID")
    box.style.width = "500px";
    box.style.height = "100px";
    box.style["backgroundColor"] = "red";
    // // 获取子页面输入框值
    console.log(window.frames["iframe1"].document.getElementById("date").value);
}
</script>

在iframe中获取父级内容

同理,在同域下,父页面可以获取子iframe的内容,那么子iframe同样也能操作父页面内容。

在iframe中,可以通过在window上挂载的几个API进行获取.

window.parent 获取上一级的window对象,如果还是iframe则是该iframe的window对象
window.top    获取最顶级容器的window对象,即,就是你打开页面的文档
window.self   返回自身window的引用。可以理解 window===window.self
<script>
    // 方法一: 父页面可控可以使用
    window.onload = function(){
      console.log(window.frames.name);
      console.log(parent.window.frames.document.querySelectorAll("iframe") );
      let arr = Array.from(parent.window.frames.document.querySelectorAll("iframe"))
      arr.forEach(item => {
        if(window.frames.name === item.name) {
          console.log(item.id);
          console.log(item.width);
          console.log(item.height);
          console.log(item.src);
        }
      })
	}
	// 方法二: 建议使用
	window.onload = function(){
      parent.window.frames.document.querySelectorAll("iframe").forEach(item => {
        if(window.frames.location.href === item.src){
          console.log(item.id, '根据地址判断-- 我是iframeA');
        }
      })
    }
</script>

动态添加 iframe

<script>
    window.onload = function(){
      let iframeCon = document.querySelector('#container')
      let text; //传递的信息
      let iframe = document.createElement('iframe')
      iframe.id = "frame"
      iframe.style = "display:block;"
      iframe.name="polling"
      iframe.src="./iframeB.html"
      iframeCon.appendChild(iframe);
    }
</script>

iframe样式

默认情况下,iframe会自带滚动条,不会全屏

去掉滚动条

<iframe name="iframe1" id="iframe1" src="./iframeA.html" scrolling="no"></iframe>

设置 iframe的高为body的高

let iframe= document.getElementById("iframe1");
let objWindow = iframe.contentWindow;
let objDoc = objWindow.document;
iframe.height = objDoc.body.offsetHeight;

是否允许iframe设置为透明,allowtransparency 默认为false

<iframe name="iframe1" id="iframe1" src="./iframeA.html" allowtransparency="true"></iframe>

是否允许iframe全屏,allowfullscreen 默认为false

<iframe name="iframe1" id="iframe1" src="./iframeA.html" allowfullscreen="true"></iframe>

iframe安全性探索

iframe出现安全性有两个方面,一个是你的网页被别人iframe,一个是你iframe别人的网页

防嵌套网页

// 使用window.top来防止自己的网页被iframe.这段代码的主要用途是限定自己的网页不能嵌套在任意网页内
if(window != window.top){
    window.top.location.href = correctURL;
}
// 如果你想引用同域的框架的话,可以判断域名
try{
  top.location.hostname;  //检测是否出错
  //如果没有出错,则降级处理
  if (top.location.hostname != window.location.hostname) { 
    top.location.href =window.location.href;
  }
} catch(e){
  top.location.href = window.location.href;
}

在服务器上,对使用iframe的权限进行设置 X-Frame-Options

X-Frame-Options是一个相应头,主要是描述服务器的网页资源的iframe权限。目前的支持度是IE8+有3个选项:
DENY:		当前页面不能被嵌套iframe里,即便是在相同域名的页面中嵌套也不允许,也不允许网页中有嵌套iframe
SAMEORIGIN: iframe页面的地址只能为同源域名下的页面
ALLOW-FROM: 可以在指定的origin url的iframe中加载

X-Frame-Options: DENY  拒绝任何iframe的嵌套请求
X-Frame-Options: SAMEORIGIN 只允许同源请求,例如网页为 foo.com/123.php,則 foo.com 底下的所有网页可以嵌入此网页,但是 foo.com 以外的网页不能嵌入
X-Frame-Options: ALLOW-FROM http://s3131212.com 只允许指定网页的iframe请求,不过兼容性较差Chrome不支持

X-Frame-Options其实就是将前端js对iframe的把控交给服务器来进行处理。

//js
if(window != window.top){
    window.top.location.href = window.location.href;
}
//等价于
X-Frame-Options: DENY

//js
if (top.location.hostname != window.location.hostname) { 
    top.location.href =window.location.href;
}
//等价于
X-Frame-Options: SAMEORIGIN
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
随着Web应用程序的不断发展,越来越多的Web开发者需要处理跨域访问的问题。尤其在网站开发中,标签<iframe>存在一些跨域问题,需要得到解决。 解决这些问题的一种方案是使用反向代理服务器。Nginx是一个功能强大的开源服务器软件,可以用来提供反向代理服务,也可以作为Web服务器、邮件服务器和负载均衡器。 在使用Nginx作为反向代理服务器时,可以按如下步骤解决iframe跨域问题: 1.安装和配置Nginx。首先,您需要在服务器上安装Nginx,并确保nginx.conf文件正确配置。配置反向代理服务器,将请求从原始服务器发送到新的服务器。 2.设置虚拟主机。为了使Nginx适用于您的网站,需要设置虚拟主机,配置主机的ip格式和端口号。通常情况下,虚拟主机可以支持多个域名和主机名,可以同时接收多个请求。 3.设置location指令。为了完成反向代理任务,可以使用location指令,将请求传递给正确的服务器,并且从指定的URL获取响应。具体而言,您需要在nginx.conf文件中指定location指令,并告诉Nginx需要向哪个服务器发送请求。 4.启用SSL。如果您的网站需要安全的传输,比如HTTPS,那么您可以使用SSL/TLS加密功能。在Nginx中配置SSL,需要使用SSL module或者OpenSSL来启用。 总之,Nginx是一个非常强大的反向代理服务器,可以很好地解决网站开发中的问题。利用其反向代理功能,您可以很容易地解决iframe跨域访问的问题,确保您的Web应用程序能够正常运行并保持安全性和高可用性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端全栈分享站

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值