iframe之父子页面通信

1、获取 子页面 的 window 对象 

在父页面中,存在如下两个对象

window.frames

document.iframeElement.contentWindow

可以获取到 子页面 window 对象

// iframe id
document.getElementById('menuIframe').contentWindow // iframe name
window.frames['menuIframe'].window // iframe index 当前窗体的第几个 iframe
window.frames[1].window

既然拿到了 window 对象,那函数和DOM就到手了。

2、子 iframe 获取 父页面

window.parent 对象

window.top对象

// 判断当前页面是否是 iframe 或 顶级页面
window.parent == window
window.top == window

window.parent 即为当前页面的上一级页面的 window 对象,如果当前页面已是 顶层 页面,则 window.parent 就是自己。

3、小实例

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<iframe src="/sub.html" name="iframeContainer" id="iframeContainer"></iframe>
<script type="text/javascript">
function parentHello() {
alert("this is parent hello function!");
}
window.frames['iframeContainer'].subHello();
</script>
</body>
</html> <!-- sub.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
function subHello() {
alert("this is sub hello function!");
} window.parent.parentHello();
</script>
</body>
</html>

  

转载自:https://my.oschina.net/sallency/blog/1618971

iframe之父子页面通信的更多相关文章

  1. iframe父子页面通信

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

  2. js Iframe与父级页面通信及IE9-兼容性

    一. postMessage window.postMessage()方法安全地启用Window对象之间的跨源通信:例如,在页面和它产生的弹出窗口之间,或者在页面和嵌入其中的iframe之间. 二.语 ...

  3. 六)iframe 及父子页面之间获取元素、方法调用

    http://www.w3school.com.cn/tags/tag_iframe.asp father.html <!DOCTYPE html> <html> <he ...

  4. 关于使用iframe的父子页面进行简单的相互传值

    当一个页面使用了iframe作为嵌套时,如何想要将父页面的数据传给iframe子页面,那iframe所指向的呢个子页面是怎么获取呢,又或者子页面的数据要给父页面使用,那么父页面又如何获取子页面的数据呢 ...

  5. HTML父子页面通信问题(showModalDialog)

    1. showModalDialog参数说明 window.showModalDialog(URL, ARGS,Features)(在父窗口中调用打开新的窗口) URL          --  必选 ...

  6. js之iframe子页面与父页面通信

    iframe子页面与父页面通信根据iframe中src属性是同域链接还是跨域链接,通信方式也不同. 一.同域下父子页面的通信 父页面parent.html <html> <head& ...

  7. js之iframe父、子页面通信

    注意事项 一 . 页面加载顺序:一般先加载完父页面才会去加载子页面,所以:必须要确保在iframe加载完成后再进行操作,如果iframe还未加载完成就开始调用里面的方法或变量,会产生错误.判断ifra ...

  8. iframe父子页面之间相互调用元素和函数

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. iframe子页面与父页面通信

    同域下父子页面的通信 父页面: <!DOCTYPE html> <html> <head lang="en"> <meta charset ...

随机推荐

  1. win10和ubuntu16.04双系统Geom Error

    报错信息: Geom Error Reboot and Select proper Boot device or Insert Boot Media in selected Boot device a ...

  2. 【web】之获取服务器tomcat下webapps目录路径

    String realPath = req.getServletContext().getRealPath("/"); String realPathParent=(new Fil ...

  3. Elasticsearch Internals: Networking Introduction An Overview of the Network Topology

    This article introduces the networking part of Elasticsearch. We look at the network topology of an ...

  4. bzoj4937: [Ceoi2016]popeala

    Description 你办了一场比赛,有n给人参加,只有一道题,有m个数据点,标号为1~m,每个测试点都有一个分数a[i].现在所 有选手已经提交了程序并且测评完了,你知道每个人都能通过哪些测试点. ...

  5. 三星GT-N8010刷机教程

    本刷机教程只针对三星GT-N8010机器(以下简称GT-N8010),以下操作本人已在GT-N8010机器上亲测,且都成功,其它机器没有测试不能保证成功. 刷机有风险,请谨慎使用!请先备份资料和信息. ...

  6. Python 解析XML实例(xml.sax)

    已知movies.xml <collection shelf="New Arrivals"> <movie title="Enemy Behind&qu ...

  7. python programming作业5

      # -*- coding: utf-8 -*- class ageError(Exception): pass class salaryError(Exception): pass class s ...

  8. 将OpenVZ系统硬盘从200G升级到400G

    平时习惯性动作df了一下,结果发现...... ‘ 200G的磁盘空间快满了(99%),只剩下2G多了. 索性一下加到400G空间 目前为止很顺利. 但是OpenVZ这台需要关机再开机才行. 重新开机 ...

  9. 无法定位序数****于动态链接库LIBEAY32.dll上

    问题出现原因: GNS3打开出现问题 尝试的方法如下: 创建脚本: @echo 开始注册 copy libeay32.dll %windir%\system32 regsvr32 %windir%\s ...

  10. ado.net 中事务的使用

    SqlHelper 类方法中启用事务 public static int UpdateByTran(List<string> sqlList) { SqlConnection conn = ...