js弹出窗⼝返回值的简单实例
a.html:
<form name="form1" method="post" action="">
<a href="javascript:void(null)" class="add" onClick="open('b.html','','resizable=1,scrollbars=1,status=no,toolbar=no,menu=no,width=500,height=400,left=150,top=50')">增加</a> <input type="text" name="text1">
</form>
b.html:
<script language="javascript" type="text/javascript">
function returnValue()
{
window.opener.1.ElementById("returnText").value;
window.close();
}
</script>
<input type="button" name="Submit" value="提交" onclick="returnValue();">
<input name="returnText" type="text" id="returnText">
</p>
补充:window.opener 的⽤法
window.opener 的⽤法在⼀般的⽤法中,只是⽤来解决关闭窗⼝时不提⽰弹出窗⼝,⽽对它更深层的了解⼀般⽐较少。其实 window.opener是指调⽤window.open⽅法的窗⼝。
在⼯作中主要是⽤来解决部分提交的。这种跨页操作对⼯作是⾮常有帮助的。
如果你在主窗⼝打开了⼀个页⾯,并且希望主窗⼝刷新就⽤这个,打开页⾯的window.opener就相当于
主窗⼝的window。
主窗⼝的刷新你可以⽤
window.load();
如果你⽤虚拟的⽬录:如struts的*.do会提⽰你重试
你可以改成这样 urformname.submit()
就好了
2〉
在应⽤中有这样⼀个情况,
在A窗⼝中打开B窗⼝,在B窗⼝中操作完以后关闭B窗⼝,同时⾃动刷新A窗⼝
function closeWin(){
hasClosed = true;
window.opener.location="javascript:reloadPage();";
window.close();
}
beforeunload(){
if(!hasClosed){
window.opener.location="javascript:reloadPage();";
弹出窗口代码编写}
}
</script>
上⾯的代码在关闭B窗⼝的时候会提⽰错误,说缺少Object,正确的代码如下:
function closeWin(){
hasClosed = true;
window.opener.location="javascript:reloadPage();";
window.opener=null;
window.close();
}
beforeunload(){
if(!hasClosed){//如果已经执⾏了closeWin⽅法,则不执⾏本⽅法
window.opener.location="javascript:reloadPage();";
}
}
</script>
reloadPage⽅法如下:
function reloadPage() {
<(0);
document.location = document.location;
load();
}
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').load();
========================================================================================最后,为了同时⽀持刷新普通⽗窗⼝和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();";
<%}%>
//p.mainFrame.location="javascript:reloadPage();";
//self.opener.frames.load(true);
window.opener = null;
window.close();
}
beforeunload(){
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.ElementById("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].ElementById('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⾥的页⾯有限制,就是必须是同域下的,否则⽆法访问
如果是同⼀域下,但是⼦域名不同,那么涉及到的js,html⽂件都加上⼀句。
document.domain = xxx [这⾥填写你的域
名]ElementById('iframeid').ElementById('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="ElementById('fromChild').value)">
在⼦窗⼝中:
function subm(){
window.ElementById('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>
以上这篇js弹出窗⼝返回值的简单实例就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。