javascript调用父窗口(父页面)的方法

2009-01-22 15:16:00

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")%>">

%>

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注