window.parent與window.opener的區別

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

2:   window.opener 是window.open 打開的子頁面調用父頁面對象

a.html

程序代碼

<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>
<input type="text" name="username" id="username"/>
<input type="button" value="彈出子頁面" onClick="openSubWin();">



b.html

程序代碼

<script type="text/javascript">
function UpdateParent()
{
var _parentWin = window.opener ;
_parentWin.form1.username.value = "xxxx" ;
}
</script>
<input type="button" name="button" id="button" value="更新主頁面的UserName內容" onClick="UpdateParent();"> 
實例地址:  http://www.cnspry.cn/blog/attachments/window.opener實例/a.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章