window.parent与window.opener的区别 javascript调用主窗口方法

在这里总结一下,javascript调用父窗口(父页面)的方法。

1:   window.parent 是iframe页面调用父页面对象

举例:

a.html

<html>
<head><title>父页面</title></head>
<body>
<form name="form1" id="form1">

<input type="text" name="username" id="username"/>

</form>
<iframe src="b.html" width=100%></iframe>
</body>
</html>

如果我们需要在b.htm中要对a.htm中的username文本框赋值,就如很多上传功能,上传功能页在Ifrmae中,上传成功后把上传后的路径放入父页面的文本框中

我们应该在b.html中写
<script type="text/javascript">
var _parentWin = window.parent ;
_parentWin.form1.username.value = "xxxx" ;
</script>


实例地址:  
http://www.cnspry.cn/blog/attachments/window.parent实例/a.html
源码:
1.a
<html>
<head><title>主页面</title></head>
<body>
<form name="form1" id="form1">
<input type="text" name="username" id="username"/>
</form>
<iframe src="b.html" width=100%></iframe>
</body>
</html>
1.b
<html>
<head><title></title>
<script type="text/javascript">
function UpdateParent()
{

var _parentWin = window.parent ;

_parentWin.form1.username.value = "xxxx" ;

}
</script>
</head>
<body>
<form name="form1" id="form1">
<p>&nbsp; </p>
<p align="center">
    <input type="button" name="button" id="button" value="更新主页面的UserName内容" onClick="UpdateParent();">
</p>
<p>&nbsp; </p>
</form>
</body>
</html>
2:   window.opener 是window.open 打开的子页面调用父页面对象

实例地址:  
http://www.cnspry.cn/blog/attachments/window.opener实例/a.html
源码:
2.a
<html>
<head>
<title>主页面</title>
<script type="text/javascript">
function openSubWin()
{
var _width = 300 ;

var _height = 200 ;
var _left = (screen.width - _width) / 2 ;
  
var _top = (screen.height - _height) / 2 ;
  
window.open("b.html",null,
"height=" + _height + ",width=" + _width + ",status=no,toolbar=no,menubar=no,location=no,resizable=yes,left=" + _left + ",top=" + _top);

}
</script>
</head>
<body>
<form name="form1" id="form1">
<input type="text" name="username" id="username"/>
<input type="button" value="弹出子页面" onClick="openSubWin();">
</form>
</body>
</html>

2.b
<html>
<head>
<title>子页面</title>
<script type="text/javascript">
function UpdateParent()
{

var _parentWin = window.opener ;

_parentWin.form1.username.value = "xxxx" ;

}
</script>
</head>
<body>
<form name="form1" id="form1">
<p>&nbsp; </p>
<p align="center">
    <input type="button" name="button" id="button" value="更新主页面的UserName内容" onClick="UpdateParent();">
</p>
<p>&nbsp; </p>
</form>

</body>
</html>

<%
Call FunMsg("upload_pic.asp?Send=OK&FileName="&SFileName&"&FileName_small="&Sfilename_small,"文件上传成功!")
   
Dim Send,StrBody
Send=Request.QueryString("Send")
FileName=Request.QueryString("FileName")
FileName_small=request.QueryString("FileName_small")
if Send="OK" And FileName<>"" And FileName_small<>"" then
StrBody="onLoad=setOpener('"&FileName&"','"&FileName_small&"')"
end if

<script type="text/javascript">
function setOpener(index,index_small) {
   window.opener.Images(index,index_small);
   window.close();
}
</script>


<body leftmargin="0" topmargin="0" <%=StrBody%>>

<script type="text/javascript">
function Images(catName,catName_small){
document.form.P_Pic.value=catName;
document.form.P_smallPic.value=catName_small;
document.form.P_PicB.src="../info/Pic/"+catName;
}
</script>


<input name="P_Pic" type="hidden" id="P_Pic" Value="<%=pRs("P_Pic")%>">
<input name="P_smallPic" type="hidden" id="P_smallPic" Value="<%=pRs("P_smallPic")%>">

%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值