1.定义

frames[]是窗口中所有命名的框架组成的数组。这个数组的每个元素都是一个Window对象,对应于窗口中的一个框架。

2.用法

假设iframe 是一个以存在的 iframe 的 ID 和 NAME 值,获取iframe的方法有:

document.getElementById(“iframe”);  (获取的是 iframe 元素,可改变iframe的 src 或者 border , scrolling 等 attributes)

window.frames[“iframe”];  //   window.frames[window.frames.length - 1]     (获取的是window窗体,可通过其 document 属性操作dom, 可使用iframe内的函数、变量)

例子:

$(window.frames["iframe"].document).find("#name").val("");

3. 扩展  iframe 跨页面通信

parent.html

<html>

<head>
<style>
h1{
color: #5dd;
}
</style> </head> <body>
<h1>parent</h1>
<input id="button" type="button" value="调用child.html中的函数say()" onclick="callChild()" />
<iframe name="myFrame" id="iframea" src="child.html" onload="load()"></iframe>
<script type="text/javascript">
function say() {
alert("parent.say");
} function callChild() {
myFrame.window.say();
myFrame.window.document.getElementById("button").value = "parent调用结束";
}
function load(){
// console.log(document.getElementById('iframea').contentDocument.getElementById('button'));
console.log(document.getElementById('iframea').contentWindow.document.getElementById('button')); } </script>
</body> </html>

child.html

<html>

<head>
<style>
body{
background-color: #666;
}
h1{
color:red;
}
</style>
</head> <body>
<h1>chlid</h1>
<input id="button" type="button" value="调用parent.html中的say()函数" onclick="callParent()" />
<script type="text/javascript">
function say() {
alert("child.say");
} function callParent() {
parent.say();
parent.window.document.getElementById("button").value = "child调用结束";
}
</script>
</body> </html>

知识点:

window.parent 获取上一级的window对象

window.top 获取最顶级容器的window对象
window.self 返回自身window的引用

iframe.contentWindow, 获取iframe的window对象

iframe.contentDocument, 获取iframe的document对象

判断iframe是否加载完成有两种方法:

1. iframe上用onload事件
2. 用document.readyState=="complete"来判断

js在iframe子页面操作父页面元素代码:  window.parent.document.getElementByIdx_x("父页面元素id");

在iframe中调用父页面中定义的方法和变量:   window.parent.window.parentMethod();      window.parent.window.parentValue;

jquery在iframe子页面获取父页面元素代码如下:    $("#objid",parent.document)

js在父页面获取iframe子页面元素代码如下:     window.frames["iframe_ID"].document.getElementByIdx_x("子页面元素id");

父页面操作iframe子页面的方法和变量      window.frames["iframe_ID"].window.childMethod();      window.frames["iframe_ID"].window.childValue;

jquery在父页面获取iframe子页面的元素       $("#objid",document.frames('iframename').document)

window.frames && iframe 跨页面通信的更多相关文章

  1. [转]html5: postMessage解决跨域和跨页面通信的问题

    [转]html5: postMessage解决跨域和跨页面通信的问题 平时做web开发的时候关于消息传递,除了客户端与服务器传值,还有几个经常会遇到的问题: 多窗口之间消息传递(newWin = wi ...

  2. iframe跨域通信实战

    "长时间不写文章,开头的方式总是那么出奇的相似",最近很忙,好久没写博客了啊(是不是?). 更换工作已经有三个月有余,这段三个月把过去三年没加过的班都加了一次.收获挺多,发现的问题 ...

  3. 利用window.name+iframe跨域获取数据详解

    详解 前文提到用jsonp的方式来跨域获取数据,本文为大家介绍下如何利用window.name+iframe跨域获取数据. 首先我们要简单了解下window.name和iframe的相关知识.ifra ...

  4. iframe 跨域问题解决方案 利用window.name+iframe跨域获取数据详解

    详解 前文提到用jsonp的方式来跨域获取数据,本文为大家介绍下如何利用window.name+iframe跨域获取数据. 首先我们要简单了解下window.name和iframe的相关知识.ifra ...

  5. JavaScript 跨域:window.postMessage 实现跨域通信

    JavaScript 跨域方式实现方式有很多,之前,一篇文章中提到了 JSONP 形式实现跨域.本文将介绍 HTML5 新增的 api 实现跨域:window.postMessage . 1 othe ...

  6. 利用HTML5的window.postMessage实现跨域通信

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp77   HTML5的window.postMessage简述 postM ...

  7. iframe父子页面通信

    一.同域下父子页面的通信 1.父页面调用子iframe页面 (1)通过iframe的Id获取子页面的dom,然后通过内置属性contentWindow取得子窗口的window对象,此方法兼容各个浏览器 ...

  8. 对iframe跨域通信的封装

    github源码:https://github.com/boycy815/topProxy 为了偷懒所以依赖了Kissy:http://docs.kissyui.com/ 用法举例:需求是在http: ...

  9. 使用window.postMessage实现跨域通信

    JavaScript由于同源策略的限制,跨域通信一直是棘手的问题.当然解决方案也有很多: document.domain+iframe的设置,应用于主域相同而子域不同: 利用iframe和locati ...

随机推荐

  1. 微信小程序 --- 获取网络状态

    获取网络状态:wx.getNetworkType btnclick:function(){ wx.getNetworkType({ success:function(res){ console.log ...

  2. Hive sql语法详解

      Hive 是基于Hadoop 构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据,可以将结构 化的数据文件映射为一张数据库表,并提供完整的SQ ...

  3. lunux系统安全

    1.清除不必要的系统帐户 [root@deep]# userdel adm [root@deep]# userdel lp [root@deep]# userdel sync [root@deep]# ...

  4. 云计算之路-阿里云上:消灭“黑色n秒”第一招——不让CPU空闲

    昨天对“黑色n秒”问题的最终猜想以失败而告终,从而让我们结束了被动猜想阶段,进入了主动进攻阶段——出招. 今天出第一招——用C#写个小程序,让其在每个CPU核上运行一个线程,不让任何一个CPU核进入空 ...

  5. 在CentOS7下从0开始搭建docker并发布tomcat项目

    一切从0开始,我也是个小白: 1.检查你的系统是不是高于3.8的内核,如果没有请升级CentOS7或者Ubuntu 14 #uname -a 2.CentOS7下安装docker #yum -y in ...

  6. Spring 体系结构

    https://www.w3cschool.cn/wkspring/dcu91icn.html 体系结构 Spring 有可能成为所有企业应用程序的一站式服务点,然而,Spring 是模块化的,允许你 ...

  7. IDEA阿里巴巴Java开发规约插件使用

    前言 2017年2月9日,首次公布<阿里巴巴Java开发手册>; 2017年9月25日,阿里巴巴集团发布了<阿里巴巴Java开发手册>PDF终极版; 2017年10月14日,在 ...

  8. ROS 命令学习记录

    #catkin_init_workspace //just get CMakeList.txt #catkin_make  //增加了信文件夹 build和devel #Catkin_creat_pk ...

  9. JOJ1202。重新操刀ACM,一天一练!做个简单的题目温习。

    http://ac.jobdu.com/problem.php?pid=1202 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100).   ...

  10. 4.5 Routing -- Setting Up A Controller

    1. 改变URL可能也会改变屏幕显示哪个模板.然而,如果它们有信息去显示,模板才是有用的. 2. 在Ember.js中,一个模板从一个controller检索信息去显示. 3. 为了告诉control ...