关于window.opener


window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接而打开了b.htm,然后我们打算在b.htm上输入一个值然后赋予a.htm上的一个id为“name”的textbox中,就可以写为:

    window.opener.document.getElementById("name").value = "输入的数据";

    对于javascrīpt中的window.opener没有很好的理解。

    为什么框架中不能使用,弹出窗口的父窗口不能在框架里面的某个页面呢?那怎样通过弹出窗口操作框架中的父窗口呢?

    opener.parent.frames['frameName'].document.all.input1.value 试试这个:)

正确使用window.open返回对象的opener

 

众所周知JavaScript中:

var win = window.open(url,windowName,...); 的使用,

而win.opener则是指向父窗口的引用

然而,有种情况却比较特别,

假如有两个窗口window1和window2

按下列步骤执行:

var win = window.open(url,windowName,...);// (window1)

var win = window.open(url,windowName,...);//(window2)

其中先后这两次打开的子窗口的windowName一样

此时你会发现在window2中的win.opener却不是指向window2的,却是指向window1.

如果你想在子窗口关闭父窗口的话,就不正确了,因此可以修改上面的执行方法为:

var win = window.open(url,windowName,...);? (window1)

win.opener = window;

var win = window.open(url,windowName,...);? (window2)

win.opener = window;

只有这样修改才OK

 通过window.showModalDialog或者.showModelessDialog弹出的页面

这种情况需要两个步骤:
1 在父窗口.showModalDialog或.showModelessDialog方法的第二个参数传递window对象
比如: window.showModelessDialog('a.htm',window);
2 在a.htm中就可以通过window.dialogArguments获取该参数
比如: window.dialogArguments.fun1();
PS:子窗口可以通过设置window.returnValue设置页面返回值

比如: window.returnValue=OK;window.close();

strRtn=window.showModalDialog(......)

这时,strRtn='ok'

例子:

1.新建两个页面 一个是 Parent.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>父窗体</title>
<script language="javascript" type="text/javascript">
function OpenWindow(){
    window.open('son.html');
}
function setValue(m_strValue){
    document.getElementById("txt_Value").value = m_strValue;
}
</script>

</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label>
  <input type="text" name="txt_Value" id="txt_Value" />
  </label>
  <label>
  <input type="button" name="btn_ShowClose" id="btn_ShowClose" value="按钮" onclick="OpenWindow();" />
  </label>
</form>
</body>
</html>

另一个是子窗体 :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>子窗体</title>
<script language="javascript" type="text/javascript" >
function CloseWind(){
    opener.setValue("传值到父窗体");
    window.close();
}
</script>

</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label>关闭
  <input type="button" name="btn_Close" id="btn_Close" value="按钮" onclick="CloseWind();"

/>
  </label>
</form>
</body>
</html>

2.通过子窗体执行的父窗体的setValue(m_strValue)来执行赋值操作.

posted on 2008-01-11 00:06  Above The Sky  阅读(246)  评论(0)    收藏  举报

导航