本文介绍: iframe标签支持 HTML 中的全局属性iframe标签支持 HTML 中的事件属性:如Window 事件属性、Form 事件、Keyboard 事件、Mouse 事件、Media 事件等iframe常用的元素属性:IFrame 对象代表 HTML iframe 元素。可使用 getElementById() 来访问 iframe 元素。如:var iframe = document.getElementById(“iframe”);var iwindow = iframe.cont

1、什么是iframe

  1. iframe是HTML元素用于网页内嵌一个网页
  2. iframe默认一个宽高存在边界。
  3. iframe是一个行内块级元素可以通过display修改
  4. 所有浏览器都支持 iframe 标签。不过版本支持情况不同。
  5. 可以需要文本放置在 <iframe 和 </iframe 之间,这样就可以应对无法理解 iframe 的浏览器
  6. 在 HTML 4.1 Strict DTD 和 XHTML 1.0 Strict DTD 中,不支持 iframe 元素。

2、iframe的元素属性

在这里插入图片描述
在这里插入图片描述

iframe常用的元素属性:

  1. src指定联网页的地址
  2. widthheight控制iframe的宽高
  3. frameborder:iframe默认有个边界,可以设置frameborder为0清除边界。
  4. name框架名称
  5. scrolling是否滚动。yesnoauto
    在这里插入图片描述

3、iframe操作

  1. 每个iframe里各自维护自己全局window对象
  2. 只有同域才能进行iframe之间的改写,跨域时,只能进行简单路由跳转
  3. 父级使用window.frames[name]可以获取子iframe的window对象,相应的可以获取document对象,从而对子iframe进行dom操作
  4. 在子iframe想要操作父元素的iframe,直接使用子元素的window.parent获取父级元素的window对象,从而获取document操作dom

4、iframe 对象及属性

在这里插入图片描述

  1. IFrame 对象代表 HTML iframe 元素。

  2. 使用 getElementById() 来访问 iframe 元素。如:
    var iframe = document.getElementById(“iframe”);
    var iwindow = iframe.contentWindow;
    var idoc = iwindow.document;等价于iframe.contentDocument;
    (1)、获取iframe的iframe对象console.log(“iframe”, iframe);
    在这里插入图片描述
    (2)、获取iframe的window对象console.log(“window”, iwindow);
    在这里插入图片描述

    (3)、获取iframe的documentconsole.log(“document”, idoc);
    在这里插入图片描述
    (4)、获取iframe的htmlconsole.log(“html”, idoc.documentElement);
    在这里插入图片描述

    (5)、获取headconsole.log(“head”, idoc.head);
    在这里插入图片描述

    (6)、获取bodyconsole.log(“body”, idoc.body);
    空

  3. 父级使用window.frames[name]结合iframe的Name属性可以获取子iframe的window对象,相应的可以获取document对象,从而对子iframe进行dom操作更便捷)。如:
    var iframes = window.frames;
    var iframe = window.frames[‘myFrame’] // 返回的就是window对象

    (1)、console.log(“iframes”, iframes);
    在这里插入图片描述
    (2)、console.log(“iframe”, iframe),与下面相同。
    在这里插入图片描述

    (3)、获取iframe的window对象console.log(“iframe”, iframe.window);
    在这里插入图片描述

    (4)、获取iframe的documentconsole.log(“document”, iframe.document)
    在这里插入图片描述

  4. 在iframe中获取父级内容
    在同域下,父页面可以获取子iframe的内容,那么子iframe同样也能操作页面内容。在iframe中,可以通过在window上挂载的几个API进行获取:
    (1)、window.parent //获取上一级的window对象,如果还是iframe则是该iframe的window对象
    (2)、window.top //获取最顶级容器的window对象,即,就是你打开页面文档
    (3)、window.self //返回自身window的引用。可以理解 window===window.self(脑残)

5、创建iframe元素

使用 document.createElement() 方法创建iframe元素:var x = document.createElement(“IFRAME”);
比如,iframe长轮询:

var iframeCon = docuemnt.querySelector('#container'),

text; //传递的信息

var iframe = document.createElement('iframe'),

iframe.id = "frame",

iframe.style = "display:none;",

iframe.name="polling",

iframe.src="target.html";

iframeCon.appendChild(iframe);

iframe.onload= function(){

var iloc = iframe.contentWindow.location,

idoc = iframe.contentDocument;

setTimeout(function(){

text = idoc.getElementsByTagName('body')[0].textContent;
console.log(text);

iloc.reload(); //刷新页面,再次获取信息,并且会触发onload函数
},2000);

}

6、iframe之间的通信

6.1、什么是主域名,什么是子域名(拓展)

6.2、iframe之间的通信

  1. 主域相同而子域不同,可以使用iframe进行解决document.domain = ‘’,指定相同的主域。比如http://www.example.com/a.htmlhttp://sub.example.com/b.html两个文件中都加上document.domain = “example.com”;
    在这里插入图片描述

  2. 我们要向指定iframe发送信息时,首先要获取该iframe自己的window对象,然后使用该window对象的postMessage发送消息

    1. 事件默认参数
      (1)、e.source消息源,消息的发送窗口/iframe。
      (2)、e.origin – 消息源的 URI(可能包含协议、域名和端口),用来验证数据源
      (3)、e.data – 发送过来的数据

    2. 使用注意点
      (1)、监听时使用window.addEventListener(“message”,(e)=>{}, false),必须保证监听的window和发送消息的window相同。
      (2)、需要确定先监听message事件,再发送的消息
      (3)、targetOrigin指定了URI的话,必须是相同的域和端口号,不然会报跨域错误
      (4)、targetOrigin使用*号的话,支持跨域我们可以通过监听消息的默认参数e.origin判断是否接收到了正确的消息。
      (5)、当我们明确知道origin是谁时,不要使用星号,当要接受信息时,先判断origin是否是我们要接受的源,再做后续的操作
      (6)、获取子元素的document时要确保子元素所有dom元素已经挂载完毕,因此在原生写法时,必须写在window的onload事件中。

    3. iframe通信示例
      (1)、 窗口1:

<!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>
    <div>窗口1</div>
    <iframe src="http://127.0.0.1:5501/2.html" frameborder="1" id='123' name="abc"></iframe>
    <script>
        window.onload = function () {
            setTimeout(() => {
                window.top.postMessage('handsome', '*')
            }, 0)
        }
    </script>
</body>
</html>
(2)、窗口2
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>窗口2</div>
    <script>
        top.addEventListener('message',(e)=>{console.log(e)},false)
    </script>
</body>
</html>
(3)、运行结果

在这里插入图片描述

7、其他

7.1、iframe自适应

  1. iframe默认自带滚动条,不会全屏。所以要想适应的话首先去掉滚动条即在iframe标签添加 scrolling=“no”。
    <iframe scrolling="no" src="./aaa" id="iframe"></iframe>
    
  2. 设置iframe的高为body的高。
     var iwindow = iframe.contentWindow;
     var idoc = iwindow.document;
     iframe.height = idoc.body.offsetHeight;
    
  3. 还可以添加其它的装饰属性。
    在这里插入图片描述
  4. 示例
    // 可以直接写在内联里面,也可以在css里面定义,不过对于广告iframe来说,样式写在属性中,是最好的。
     <iframe id="frame" name="frame" width="160" height="600" frameborder="0" src="target.html" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true"></iframe>
    

7.2、防嵌套网页

7.2.1、浏览器

7.2.2、服务器

  1. X-Frame-Options
    • X-Frame-Options是一个相应头,主要是描述服务器的网页资源的iframe权限。目前的支持度是IE8+。有3个选项
      1. X-Frame-Options: DENY。拒绝任何iframe的嵌套请求
      2. X-Frame-Options: SAMEORIGIN。只允许同源请求。例如网页为 lgg.com/123.html,則 lgg.com 底下的所有网页可以嵌入此网页,但是 lgg.com 以外的网页不能嵌入
      3. X-Frame-Options: ALLOW-FROM。只允许指定网页的iframe请求,不过兼容性较差Chrome不支持
    • X-Frame-Options其实就是将前端js对iframe的把控交给服务器来进行处理。
    // 等价于X-Frame-Options: DENY
    if(window != window.top){
      window.top.location.href = window.location.href;
    }
    // 等价于X-Frame-Options: SAMEORIGIN
    if(top.location.hostname != window.location.hostname) {
      top.location.href =window.location.href;
    }
    
    • 该属性是对页面的iframe进行一个主要限制。另外还有一个Content Security Policy,同样也可以对iframe进行限制,而且,他应该是以后网页安全防护的主流。
  2. sandbox
    <iframe sandbox src="..."></iframe>
    
    <iframe sandbox="allow-forms allow-same-origin allow-scripts" src="..."></iframe>
    

    在这里插入图片描述

7.3、iframe使用场景

  1. PDF文档预览
  2. 插入广告等。

原文地址:https://blog.csdn.net/weixin_44767973/article/details/128201419

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_15297.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注