六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 63|回复: 0

JAVASCRIPT访问父窗口的变量或方法

[复制链接]

升级  13.33%

16

主题

16

主题

16

主题

秀才

Rank: 2

积分
70
 楼主| 发表于 2013-2-7 17:35:10 | 显示全部楼层 |阅读模式
javascript中,当父窗口打开一个新窗口,当前新窗口怎么去访问父窗口的变量或方法呢?
调用函数、变量的方法一样,都是需要获得父窗口的window对象,然后通过window.method()或者 window.variable 来调用
 
Sample:
A页面:
 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
<title></title>  
<script type="text/javascript">  

var array=new Array();
array.push("A");
array.push("A");


function sayHello()  
{  
    alert("hello, method");  
}  

function openBtnClicked()  
{  
    var newwin=window.open('B.html',"test","toolbar=no,location=no,top=100,left=100,directories=no,status=yes,menubar=no,scrollbars=yes,location=no,resizable=yes,width=300,height=200");  
    newwin.focus();  
}    
</script>  
</head>  

<body>  
<input type="button" name="openBtn" value="打开窗口"  />  
</body>  
</html>
 
 
 
B页面:
 <script>
       var obj=window.opener.parent;
       alert(obj.sayHello() );
       alert(obj.array);  
  </script>

 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表