博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

javascript opener 用法

Posted on 2011-01-19 10:59  itcfj  阅读(2297)  评论(0编辑  收藏  举报

javascript opener 用法 

20090612 星期五 10:27

window.opener 的用法在一般的用法中,只是用来解决关闭窗口时不提示弹出窗口, 而对它更深层的了解一般比较少。其 实 window.opener是指调用window.open方法的窗口。
在工作中主要是用来解决部分提交的。这种跨页操作对工作是非常有帮助的。
如果你在主窗口打开了一个页面,并且希望主窗口刷新就用这个,打开页面的window.opener就相当于
主窗口的window
主窗口的刷新你可以用
window.opener.location.reload()

如果你用虚拟的目录:如struts*.do会提示你重试

你可以改成这样 window.opener.yourformname.submit()
就好了

2

在应用中有这样一个情况,
A窗口中打开B窗口,在B窗口中操作完以后关闭B窗口,同时自动刷新A窗口


function closeWin(){
hasClosed = true;
window.opener.location="javascript:reloadPage();";
window.close();
}
function window.onbeforeunload(){
if(!hasClosed){
window.opener.location="javascript:reloadPage();";
}
}

</script>
上面的代码在关闭B窗口的时候会提示错误,说缺少Object,正确的代码如下:
function closeWin(){
hasClosed = true;
window.opener.location="javascript:reloadPage();";
window.opener=null;
window.close();
}
function window.onbeforeunload(){
if(!hasClosed){//
如果已经执行了closeWin方法,则不执行本方法
window.opener.location="javascript:reloadPage();";
}
}

</script>
reloadPage
方法如下:
function reloadPage() {
history.go(0);
document.execCommand("refresh")
document.location = document.location;
document.location.reload();
}
PS:
由于需要支持正常关闭和强制关闭窗口时能捕捉到事件,用了全局变量hasClosed

==============================================

补充,在父窗口是frame的时候在刷新父窗口的时候会出现问题:

The page cannot be refreshed without resending the information.
后修改如下:
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
不需要执行自带的reload()方法,注意,不要再画蛇添足加上这一句:

window.opener.parent.document.frames.item('mainFrame').location.reload();

========================================================================================
最后,为了同时支持刷新普通父窗口和frame父窗口,代码如下:
function closeWin() {
hasClosed = true;
<%if(null != frame){%>
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
<%}else{%>
window.opener.location = "javascript:reloadPage();";
<%}%>
//window.opener.top.mainFrame.location="javascript:reloadPage();";
//self.opener.frames.mainFrame.location.reload(true);
window.opener = null;
window.close();
}
function window.onbeforeunload(){
if (!hasClosed) {
<%if(null != frame){%>
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
<%}else{%>
window.opener.location = "javascript:reloadPage();";
<%}%>
window.opener = null;
}
}







     window.opener
的用法
    window.opener
返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接而打开了b.htm,然后我们打算在b.htm上输入一个值然后赋予a.htm上的一个id“name”textbox中,就可以写为:
   
    window.opener.document.getElementById("name").value = "
输入的数据";
   
   
   
对于javascript中的window.opener没有很好的理解。
   
为什么框架中不能使用,弹出窗口的父窗口不能在框架里面的某个页面呢?那怎样通过弹出窗口操作框架中的父窗口呢?
   
    opener.parent.frames['frameName'].document.all.input1.value
试试这个:)
  

frame框架里的页面要改其他同框架下的页面或父框架的页面就用parent
window.opener
引用的是window.open打开的页面的父页面。

window.frames对象可以引用iframe里的页面,也可以引用frameset里的页面.

可以这样
window.frames[0].document.getElementById('xx');
可以这样
window.frames[0].document.body.innerHTML;

frm = window.parent.window.frames['uploadFrame'];
frmDocument = frm.document;
frm.sb(3); //sb
uploadFrame页面里的一个函数

对于firefox
如果你遇到报错:parent.document.frames has no properties
换为如下代码就可以了,这个代码IE,ff兼容. frm = window.parent.window.frames['uploadFrame'];其实 frames 集合并不是挂在 document 而是挂在 window 对象下.

注意这样修改frame里的页面有限制,就是必须是同域下的,否则无法访问
如果是同一域下,但是子域名不同,那么涉及到的jshtml文件都加上一句。
document.domain = xxx.com [
这里填写你的域名]

document.getElementById('iframeid').contentWindow.document.getElementById('someelementid');




:

在父窗口window.open()一个子窗口。并定义一个变量i
在子窗口输入一个值j然后window.opener.i=j;
这样能传过去。但我在子窗口最后加了个window.close();就无法传值了。
请问是否有办法解决这个问题。使我传递值之后再关闭子窗口。
代码如下:
父窗口:parent.jsp
<script>
var i;
window.open('<%=contextPath%>/usermanage/newscontrol/cd.jsp);
</script>
<input type="button" onclick="alert(i)">
子窗口:cd.jsp
<script>
function subm(){
window.opener.i=5;
window.close();
}
</script>
<input type="button" onclick="subm()">


最佳答案

你可以在父窗口放一个
<input id="fromChild" type="hidden" />
<input type="button"
onclick="alert(document.getElementById('fromChild').value)">

在子窗口中:
function subm(){
window.opener.document.getElementById('fromChild').value=5;
window.close();
}

这样既可







<head>
<script language=javascript>
function windowclose()
{ window.opener=null;
window.close();
}
</script>
</head>

<body>
<input id="Button1" type="button" value="button" onclick="windowclose()" />

</body>

javascript脚本中openerparent的区别

2008-06-02 22:23

opener即谁打开我的,比如A页面利用window.open弹出了B页面窗口,那么A页面所在窗口就是B页面的

opener
,在B页面通过opener对象可以访问A页面。

parent
表示父窗口,比如一个A页面利用iframeframe调用B页面,那么A页面所在窗口就是B页面的

parent




JS中,window.opener只是对弹出窗口的母窗口的一个引用。比如:
a.html
中,通过点击按钮等方式window.open出一个新的窗口b.html。那么在b.html中,就可以通过

window.opener
(省略写为opener)来引用a.html,包括a.htmldocument等对象,操作a.html的内容。
假如这个引用失败,那么将返回null。所以在调用opener的对象前,要先判断对象是否为null,否则会

出现对象为空或者不存在JS错误。

<html>
<body>
<form. name=form1>
<input type=text name=inpu >
<input type=button   >
</form>
</body>
</html>


--------------------------------
back2opener.html
--------------------------------
<html>
<body>
<form. name=form1>
<input type=text name=inpu >

   <a class=under href=# >
添加</a>
</form>
</body>
</html>



window.opener
返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接而打开了

b.htm
,然后我们打算在b.htm上输入一个值然后赋予a.htm上的一个id“name”textbox中,就可以

写为:

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

子窗体给父窗体传值 javascript opener

2009-08-07 09:10

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)来执行赋值操作