JS 子窗口向父窗口传值

首先我们来了解下window对象的的方法open:

1,open() 方法用于打开一个新的浏览

window.open(URL,name,features,replace)

重要事项:请不要混淆方法 Window.open() 与方法 Document.open(),这两者的功能完全不同。为了使您的代码清楚明白,请使用 Window.open(),而不要使用 open()。

2,opener

opener 属性是一个可读可写的属性,可返回对创建该窗口的 Window 对象的引用。---这句话怎么理解呢,例如

opener即谁打开我的,比如A页面利用window.open弹出了B页面窗口,那么A页面所在窗口就是B页面的opener,在B页面通过opener对象可以访问A页面。

注意:要求父窗口打开子窗口时须使用window.open方法才能使用window.opener回传值。

   window.opener.document.getElementById("Downloads").value = '新值';

附上一个子窗口向父窗口传值的例子

复制代码
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>js实现子窗口传值到父窗口--父页面</title>
 6 </head>
 7 <body>
 8 <!--open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口。-->
 9 <input type="button" value="打开子窗口" onclick="open_win('children.html','children','800','600')"/>
10 
11 <input value="333" id="Downloads" />
12 
13 <script>
14     function open_win(url, title, w, h) {
15         var iWidth = w;
16         var iHeight = h;
17         var iTop = (window.screen.availHeight-30-iHeight)/2;       //获得窗口的垂直位置;
18         var iLeft = (window.screen.availWidth-10-iWidth)/2;           //获得窗口的水平位置;
19         myWindow  = window.open( url, title, 'height=' + iHeight + ',innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no, titlebar=yes, alwaysRaised=yes');
20     }
21 
22 </script>
23 </body>
24 </html>
复制代码
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js实现子窗口传值到父窗口--子页面</title>
</head>
<body>
我是子窗口
<script>
    window.onload = function(){
        window.opener.document.getElementById("Downloads").value = '新值';
    }
</script>
</body>
</html>
复制代码

注意:这两个文件不能再本地直接打开,会报错:必须要在架设在服务器上

children.html:11 Uncaught DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame.

opener 属性非常有用,创建的窗口可以引用创建它的窗口所定义的属性和函数。

window.opener

注释:只有表示顶层窗口的 Window 对象的 operner 属性才有效,表示框架的 Window 对象的 operner 属性无效。

3,再来谈谈opener与parent的区别 并附上top

window.parent 是iframe页面调用父页面对象,当我们想从iframe内嵌的页面中访问外层页面是可以直接利用window.parent获取,window.opener 是window.open或超链接<a> 打开的子页面调用父页面对象

parent表示父窗口,parent用于在iframe,frame中生成的子页面中访问父页面的对象。比如一个A页面利用iframe或frame调用B页面,那么A页面所在窗口就是B页面的parent

top 属性返回最顶层的先辈窗口。

该属性返回对一个顶级窗口的只读引用。如果窗口本身就是一个顶级窗口,top 属性存放对窗口自身的引用。如果窗口是一个框架,那么 top 属性引用包含框架的顶层窗口。

window.top

可应用于弹框跳出框架

1
2
3
4
5
6
7
8
9
10
11
12
function breakout()
   {
   if (window.top!=window.self)
     {
     window.top.location="/example/hdom/tryjs_breakout.htm"
     }
   }
</ script >
</ head >
< body >
 
< input  type="button" onclick="breakout()" value="跳出框架!">

  附:Window对象、Parent对象、Frame对象、Document对象和Form对象的阶层关系 
    Window对象→Parent对象→Frame对象→Document对象→Form对象,
    如下: parent.frame1.document.forms[0].elements[0].value;

4,模态窗口与父窗口通信

目前谷歌浏览器不支持showModelDialog

通过使用showModelDialog(),及showModelessDialog()方法创建的子窗口想与父窗口通信时,不能通过window.opener

来获取父窗口对象。要实现通信,必须在创建模态子窗口时向子窗口传入父窗口对象。

vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures]) 

vReturnValue = window.showModelessDialog(sURL [, vArguments] [,sFeatures]) 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

URL--必选参数,类型:字符串。用来指定对话框要显示的文档的URL。 
vArguments--可选参数,类型:变体。用来向对话框传递参数。传递的参数类型不限,包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。 
sFeatures-- 可选参数,类型:字符串。用来描述对话框的外观等信息,可以使用以下的一个或几个,用分号“;”隔开。 
1.dialogHeight :对话框高度,不小于100px,IE4中dialogHeight 和 dialogWidth 默认的单位是em,而IE5以上是px,为方便其见,在定义modal方式的对话框时,用px做单位。 
2.dialogWidth: 对话框宽度。 
3.dialogLeft: 离屏幕左的距离。 
4.dialogTop: 离屏幕上的距离。 
5.center: {yes | no | 1 | 0 }:窗口是否居中,默认yes,但仍可以指定高度和宽度。 
6.help: {yes | no | 1 | 0 }:是否显示帮助按钮,默认yes。 
7.resizable: {yes | no | 1 | 0 } 〔IE5+〕:是否可被改变大小。默认no。 
8.status: {yes | no | 1 | 0 } 〔IE5+〕:是否显示状态栏。默认为yes[ Modeless]或no[Modal]。 
9.scroll:{ yes | no | 1 | 0 | on | off }:指明对话框是否显示滚动条。默认为yes。 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

例子:子页面向父页面传值

父页面

复制代码
<body>
<!--open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口。-->
<input type="button" value="打开子窗口" onclick="open_win()"/>

<input value="333" id="Downloads" />

<script>
    function open_win(){
        var str =window.showModalDialog("children.html","","dialogWidth=400px;dialogHeight=500px");
        alert(str);
    }
</script>
复制代码

子页面:

复制代码
<body>
我是子窗口
    <script type="text/javascript">
    window.returnValue="http://www.jb51.net";
</script>
</body>
复制代码

 附上一篇:iframe层获取父页面元素,父页面获取iframe层元素的博客:

http://www.cnblogs.com/LYshuqian/p/3312383.html

猜你喜欢

转载自blog.csdn.net/czh500/article/details/80329050
今日推荐