博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
#iframe系列问题#
阅读量:7125 次
发布时间:2019-06-28

本文共 2627 字,大约阅读时间需要 8 分钟。

父页面获取子页面的window对象

/** * 在父页面获取iframe的window对象 * chrome:iframeElement. contentWindow  * firefox: iframeElement.contentWindow  * ie6:iframeElement.contentWindow */function getIframeWindow(iframeElement) {    return iframeElement.contentWindow;    //   return iframeElement.contentWindow || iframeElement.contentDocument.parentWindow;}

父页面获取子页面的document对象

/** * 在父页面获取iframe的document * chrome:iframeElement.contentDocument * firefox:iframeElement.contentDocument * ie:element.contentWindow.document * 备注:ie没有iframeElement.contentDocument属性。 */function getIframeDocument(element) {    console.dir(element);    return element.contentDocument || element.contentWindow.document;};

iframe何时装载完毕

/** * 下面的代码能正常运行于所有的浏览器之上。 * 由于iframe元素包含于父级页面中,因此以上方法均不存在跨域问题。 * 实际上IE提供了onload事件,但必须使用attachEvent进行绑定。 */function iframeOnload() {    var iframe = document.createElement("iframe");    iframe.src = "simpleinner.htm";    if (iframe.attachEvent) {        iframe.attachEvent("onload", function () {            alert("Local iframe is now loaded.");        });    } else {        iframe.onload = function () {            alert("Local iframe is now loaded.");        };    }    document.body.appendChild(iframe);}

利用onload和attachEvent,实现iframe高度自适应

/**     * 如果iframe的高度不足屏幕可视区域的高度,则iframe的高度 === 屏幕可视区域的高度     * 如果iframe的高度大于屏幕可视区域的高度,则iframe的高度 === iframe自己的高度     *       */    function setFrameHeight(iframe) {        var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;        var offsetHeight = iframeDoc.childNodes[1].offsetHeight;        var clientHeight = document.documentElement.clientHeight||document.body.clientHeight;        iframe.height = offsetHeight > (clientHeight-35) ? offsetHeight : (clientHeight-35);    }    function iframeOnload() {        var iframe = document.getElementById("iframe");        if (iframe.attachEvent) {            iframe.attachEvent("onload", function () {                setFrameHeight(this);            });        } else {            iframe.onload = function () {                setFrameHeight(this);            };        }    }

在子页面中获取父页面的window对象

/** * 存在跨域问题 * 在子页面中获取父页面的window对象 * 父页面:window.parent * 适用于所有浏览器 */console.log(window.parent);

在子页面中获取顶层页面

/** * 存在跨域问题 * 在子页面中获取顶层页面 * 顶层页面:window.top * 适用于所有浏览器 */console.log(window.top);

在子页面中获取iframe的html

/** * 存在跨域问题 * 在子页面中获取iframe的html * window.frameElement * (类型:HTMLElement) * 适用于所有浏览器 */console.log(window.frameElement);

BUG##Blocked a frame with origin "null" from accessing a cross-origin frame.

跨页面操作涉及域的概念(origin),错误的意思是:未捕获的安全错误:阻止了一个域为null的frame页面访问另一个域为null的页面。代码运行时在本地直接用浏览器打开的,地址栏是file:///的页面,只需改为localhost访问就行。

转载地址:http://lieel.baihongyu.com/

你可能感兴趣的文章
IP SLA冗余切换
查看>>
关于安全运维中,网络及安全设备基线设置的方法和必要性。
查看>>
nodejs渐入佳境[24]-用户权限-express+mongoDB+authtoken
查看>>
关于GITLAB若干权限问题
查看>>
强大的PDF创建和管理控件ActivePDF Toolkit
查看>>
linux下DNS的配置
查看>>
Android中带分割线的九宫格
查看>>
修改progressbar的样式
查看>>
node js 打包文件
查看>>
我的友情链接
查看>>
Windows sever 2008 R2 ---虚拟机安装
查看>>
PC 加入AD域的要求
查看>>
B-tree vs B+tree
查看>>
Eclipse中10个最有用的快捷键组合
查看>>
LAMP一键安装脚本
查看>>
vsphere层级架构
查看>>
我的友情链接
查看>>
转载-Linux新人必读,Linux发行版选择和软件安装的一些原则性问题
查看>>
Linux从入门到精通之监控软件Cacti
查看>>
徹底解決 Windows Server 2012 R2 惱人的輸入法問題
查看>>