JavaScript 父子页面相互调用总结
父子页面相互调用是一个在开发中经常遇到的问题,但是没有找到过比较全面的文章介绍。在此总结下来,供大家参考。
四种方式
一般情况下,我们可以使用iframe、window的open、showModalDialog、showModelessDialog方法这四种方式打开一个子窗口。(showModalDialog、showModelessDialog是IE独有的。)
下面分这四种方式来讨论如何父子页面互相调用。
分情况讨论
iframe
在这种情况下,子页面直接通过parent.var就可以对父页面的变量和方法进行操作。
父页面可以通过拿到iframe的contentWindow对象来访问子页面的window。
父页面代码,文件名为iframe.html。
|
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <script> var testVar = "我是父窗口测试变量"; var childFrame; function getChildVar(){ var childFrame = document.getElementById("childFrame"); var childWindow = childFrame.contentWindow alert(childWindow.testVar); } </script> <iframe id="childFrame" name="childFrame" frameBorder="0" src="iframe.child.html" style="border:1px solid black;"> </iframe> <br /> <button onclick="getChildVar();">拿到子页面的变量</button> </body> </html> |
子页面代码,文件名为iframe.child.html。
|
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <script> var testVar = "我是子窗口测试变量"; </script> 我是在IFrame中的子窗体。 <button onclick="alert(parent.testVar);">拿到父页面的testVar</button> </body> </html> |
open
使用window.open打开的子页面,在子页面中可以通过window.opener来访问父页面的window对象。
在父页面中,可以通过window.open方法的返回值拿到子页面的window,就可以操作子页面的变量和方法。
父页面代码,文件名为window.open.html。
|
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>window.open父页面</title> </head> <body> <script> var testVar = "我是父窗口测试变量"; var childFrame; function openWindow(){ childFrame = window.open("window.open.child.html"); } function getChildVar(){ alert(childFrame.testVar); } </script> <button onclick="openWindow();">使用window.open打开子页面</button> <button onclick="getChildVar();">拿到子页面的变量</button> </body> </html> |
子页面代码,文件名为window.open.child.html。
|
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>window.open子页面</title> </head> <body> <script> var testVar = "我是子窗口测试变量"; alert(window.opener.testVar); </script> </body> </html> |
showModalDialog
使用showModalDialog打开的子页面,如果想访问父页面的window,需要在执行showModalDialog方法的时候,把父页面的window当作参数传递过去。见父页面的代码。
因为showModalDialog是阻塞的,父页面的代码在子页面不关闭之前无法继续执行,所以只能通过returnValue拿到子页面的变量,见子页面的代码。
父页面代码,文件名为ShowModalDialog.html。
|
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>ShowModalDialog父页面</title> </head> <body> <script> var testVar = "我是父窗口测试变量"; function openDialog(){ var testVar = showModalDialog("ShowModalDialog.Child.html",window); alert(testVar); } </script> <button onclick="openDialog();">OpenDialog</button> </body> </html> |
子页面代码,文件名为ShowModalDialog.Child.html。
|
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>ShowModalDialog子页面</title> </head> <body> <script> var testVar = "我是子窗口测试变量"; function getParent(){ var parentWindow = window.dialogArguments; alert(parentWindow.testVar); } function closeMe(){ returnValue = testVar; window.close(); } </script> 我是使用ShowModalDialog打开的子页面。 <br /> <button onclick="getParent()">getParent</button> <button onclick="closeMe()">closeMe</button> </body> </html> |
showModelessDialog
使用showModelessDialog打开的子页面,同样需要在执行方法的时候,把父页面的window当作参数传递过去。
但不同之处在于showModelessDialog会直接返回子页面的window对象,不是阻塞的,可以直接对子页面的方法和变量进行访问。
父页面代码,文件名为ShowModelessDialog.html:
|
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>ShowModalDialog父页面</title> </head> <body> <script> var testVar = "我是父窗口测试变量"; function openDialog(){ var childWindow = showModelessDialog("ShowModelessDialog.Child.html",window); alert(childWindow.testVar); } </script> <button onclick="openDialog();">OpenDialog</button> </body> </html> |
子页面代码,文件名为ShowModelessDialog.html。
|
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>ShowModalDialog子页面</title> </head> <body> <script> var testVar = "我是子窗口测试变量"; function getParent(){ var parentWindow = window.dialogArguments; alert(parentWindow.testVar); } function closeMe(){ returnValue = testVar; window.close(); } </script> 我是使用ShowModalDialog打开的子页面。 <br /> <button onclick="getParent()">getParent</button> <button onclick="closeMe()">closeMe</button> </body> </html> |
JavaScript 父子页面相互调用总结的更多相关文章
- JQuery javascript实现父子页面相互调用
javascript实现父子页面相互调用 By:授客 QQ:1033553122 场景1 父页面调用子页面 如上图,在iframe子页面的<script>元素中,定义了taskStatus ...
- iframe父子页面相互调用方法,相互获取元素
父页面获取子页面 var childWin = document.getElementById('setIframe').contentWindow;//获取子页面窗口对象 childWin.send ...
- ASP.net关于C#代码与javaScript函数的相互调用
C#代码与javaScript函数的相互调用 问:1.如何在JavaScript访问C#函数?2.如何在JavaScript访问C#变量?3.如何在C#中访问JavaScript的已有变量?4.如何在 ...
- Flex父子窗口相互调用
Flex父子窗口相互调用 1.设计思路 (1)子窗口调用父窗口的方法 (2)子窗口做了修改后,返回父窗口,父窗口调用子窗口函数 2.设计源码 (1)父窗口 ParentWindow.mxml: < ...
- Flex父子窗体相互调用
Flex父子窗体相互调用 1.设计思路 (1)子窗体调用父窗体的方法 (2)子窗体做了改动后,返回父窗体,父窗体调用子窗体函数 2.设计源代码 (1)父窗体 ParentWindow.mxml: &l ...
- vue中的父子组件相互调用
vue中的父子组件相互调用: 1.vue子组件调用父组件方法:子组件:this.$emit('xx'); 父组件:定义yy方法,并在引用子组件时传参,如@xx="yy" 2.vue ...
- iframe 父子页面方法调用
在写代码的时候经常会用到将一个网页嵌入到另一个网页中,w3c也规定了一个标签<iframe>,这个标签本身就支持跨域,而且所有的浏览器都支持 iframe具有以下属性: 1.framebo ...
- C++和JavaScript脚本的相互调用
脚本调用C++相对比较容易,使用ATL组件只需要抛双接口即可,但在exe里如何做到呢?本文实现了在exe里脚本和C++的相互调用.在EXE里也需要对外抛送一个继承自IDispatch的接口.并需要重载 ...
- JavaScript父子页面之间的相互调用
父页面: <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>< ...
随机推荐
- VMware Workstation Pro 安装centos6.5
先要安装好VMware Workstation Pro破解版本,下载好centos系统.iso光盘映像文件,本人下载的是CentOS-6.5-i386-bin-DVD1.iso 添加新的虚拟主机可以通 ...
- 从Javascript单线程谈Event Loop
假如面试回答js的运行机制时,你可能说出这么一段话:"Javascript的事件分同步任务和异步任务,遇到同步任务就放在执行栈中执行,而碰到异步任务就放到任务队列之中,等到执行栈执行完毕之后 ...
- AI类人工智能产品经理的丛林法则
AI是大家都很关注的领域,然而对于大部分想要入行的同学来讲,AI的算法技术门槛相对较高,让很多空有热血但是缺少数学背景的同学望而却步.不知道什么时候,可能是“人人都是产品经理”这个论调的影响,产品经 ...
- 多线程(五) java的线程锁
在多线程中,每个线程的执行顺序,是无法预测不可控制的,那么在对数据进行读写的时候便存在由于读写顺序多乱而造成数据混乱错误的可能性.那么如何控制,每个线程对于数据的读写顺序呢?这里就涉及到线程锁. 什么 ...
- NetBeans主题配色方案加设置.md
1.网上下载主题的地址是:http://netbeansthemes.com/ **上面的主题我看了下但是感觉不太适合自己所以自己就仿照的设置了下下面我附上自己的配置信息 感兴趣的可以下载下来自己看下 ...
- Java中parseInt()和valueOf(),toString()的区别
1.parseInt()就是把String类型转化为int类型. 如 String a= "123"; int b = Integer.parseInt(a); 这样b就等于123 ...
- axios.js
Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios ...
- Shrio认证详解+自定义Realm
Authentication(身份认证)是Shiro权限控制的第一步,用来告诉系统你就是你. 在提交认证的时候,我们需要给系统提交两个信息: Principals:是一个表示用户的唯一属性,可以是用户 ...
- 【Tomcat】停止冗余进程
tomcat多次连续启动,现在通过脚本,一次执行,全部停掉 [root@YFDT-DEM1701-EPMDM ~]# cat tomcat_pid.sh #!/bin/bash cat /dev/nu ...
- 【Ubuntu 16】安装eclipse
1.将eclipse.tar.gz传送到/home/xxx/下,解压缩,这里我已经配置好了JDK1.7,所以eclipse配置了就可以使用 2.创建快捷方式 dream361@master:~$ to ...