获取iframe的window对象js代码如下.注意:一定要在文档加载完成之后,才能获取到
var Iframe=document.getElementById("script");//先获取到iframe元素
var iframeWindow=(Iframe.contentWindow || Iframe.contentDocument);//获取到指定iframe下window

对应html代码:

<iframe  class="Panel" id="script" src="t.html"></iframe>

以上代码就能实现获得页面中iframe的window对象

现在实现iframe和父页面通信,

page1(为iframe页面代码):

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8" />
<title>By:DragonDean</title>
<script src="jquery.js"></script>
<script type="application/javascript">
window.onload=function(){
window.evt=document.createEvent('Event');//创建一个事件,挂在当前页面的window对象上
window.evt.initEvent('myEvent',true,true);//初始这个事件
var obj=document.getElementById('testBtn');
obj.addEventListener('click', function(){//自定义事件触发的条件,例子中用的点击
window.evt.test='测试iframe之间用事件传数据';//测试传值
window.dispatchEvent(window.evt);//让自定义事件触发
}, false);
    console.log(parent);//父页面的window对象,iframe嵌入页面自带
    };
  
</script></head>
<body>
<button id="testBtn">test</button>
</body>
</html>

page2(主页面):

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script>
window.onload=function(){
var Iframe=document.getElementById("script");//先获取到iframe元素
var iframeWindow=(Iframe.contentWindow || Iframe.contentDocument);//获取到指定iframe下window
/*在主页面对iframe页面的window上添加一个监听事件,监听自定义事件,传入一个当前页面的函数,获取iframe触发的事件*/
iframeWindow.addEventListener('myEvent',function(event){//event为t.html中触发这个监听的window.evt事件
console.log(event.test);//到此,传值完成
})
}
</script>
</head>
<body>
<iframe class="Panel" id="script" style="height: 1000px;" src="page1.html" name="script"></iframe>
</body>
</html>

将两个页面放同目录下,运行page2,呼出控制台,就能看到传值结果。

html,获取iframe的window,document,自定事件与iframe通信的更多相关文章

  1. SharePoint 2013 页面中window/document.onload/ready 事件不能触发的解决方案

    问题1:在SharePoint 2013页面中使用Javascript 事件window/document.onload/ready时,你会发现处理onload/ready事件的代码根本不能执行. 问 ...

  2. 获取iframe的window对象

    在父窗口中获取iframe中的元素 // JS // 方法1: var iframeWindow = window.frames["iframe的name或id"]; iframe ...

  3. js 浏览器窗口大小改变 高度 宽度获取 window/document.height()区别

    <script> //当浏览器的窗口大小被改变时触发的事件window.onresize window.onresize = function(){ console.log($(windo ...

  4. jQuery 获取父窗口的元素 父窗口 子窗口(iframe)

    $("#父窗口元素ID",window.parent.document); 对应javascript版本为window.parent.document.getElementById ...

  5. JavaScript(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作

    一.Iframe 篇 公共部分 //父对象得到子窗口的值 //ObjectID是窗口标识,ContentID是元素ID function GetValue(ObjectID,ContentID) { ...

  6. 总结JavaScript(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作

    一.Iframe 篇 //&&&&&&&&&&&&&&&&&&a ...

  7. 总结js(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作

    http://hi.baidu.com/yashua839/blog/item/131fdb2fe547ef221f3089af.html一.Iframe 篇 //&&&&am ...

  8. 基本的window.document操作及实例

    基本的window.document操作及实例 找元素 1.根据id找 var d1 = document.getElementById("d1"); alert(d1); 2.根 ...

  9. Window.document对象

    1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:     var a =docunme ...

随机推荐

  1. BZOJ4890 & 洛谷3761:[TJOI2017]城市——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4890 https://www.luogu.org/problemnew/show/P3761 从加 ...

  2. LOJ2350:[JOI2017/2018决赛]月票购买——题解

    https://loj.ac/problem/2350 比较简单的题,为什么我实现得这么sb? 第一个包其实已经给了提示(第一个包的解法就是在S->T所有最短路径上的所有点到V的最短路的最小值. ...

  3. 1.61 三角形O(nlogn)做法

     书里给出比较无脑的做法,三个for循环复杂度是n的立方.如果先把数列排序,依次判断连续三个数是否能形成三角形,可以把时间复杂度控制在nlogn. #include<stdio.h> ...

  4. MySQL5.6之Index Condition Pushdown(ICP,索引条件下推)-Using index condition

    http://blog.itpub.net/22664653/viewspace-1210844/ -- 这篇博客写的更细,以后看 ICP(index condition pushdown)是mysq ...

  5. android源码了解dex加载流程

    源码版本:4.4.4_r1 http://www.cnblogs.com/lanrenxinxin/p/4712224.html http://wangzhixian.org/Others/DexCl ...

  6. push与createElement性能比较

    下面的实验是验证push()函数与CreateElement()函数在创建HTML元素的效率.可以看出,实用push()确实效率要比后者要高,不过究竟可以高出多少,还需要有专研精神的朋友去测试了,这里 ...

  7. linux 常见服务端口

    Linux服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程(daemons) 来执行的.守护进 ...

  8. @Autowired @Resource @Qualifier的区别

    参考博文: http://www.cnblogs.com/happyyang/articles/3553687.html http://blog.csdn.net/revent/article/det ...

  9. DOM案例五星评分控件

    模仿网页上评分的五个五星. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...

  10. http-反向代理学习

    主要是学习了反向代理. 结合公司的方向代理使用,然后与同事进行交流,知识还是需要通过交流才能印象深刻,以后多多交流.