javascript中的opener和parent

最近在復習JavaScript,看到關於frame框架和window窗口部分時候,部分概念有點混淆,在此記錄一下。

opener

在當前窗口創建子窗口,可能需要從子窗口引用父窗口,因此就有瞭opener的存在。

opener即誰打開我的,比如A頁面利用window.open彈出瞭B頁面窗口,那麼A頁面所在窗口就是B頁面的opener,在B頁面通過opener對象可以訪問A頁面。

parent

在當前窗口中包含frame框架,在一組框架集中,子框架訪問父框架時,就需要parent來訪問。

parent表示父窗口,比如一個A頁面利用iframe或frame調用B頁面,那麼A頁面所在窗口就是B頁面的parent。

綜上所述,opener是負責窗口之間父子關系,parent是負責框架間父子關系。

實例

 

	Test Parent And Opener
	<script type="text/javascript">
		var newWindow = window.open("https://www.w3school.com.cn",'','width=400,height=200');

		window.onload = function(){
			alert("newWindow.opener: "+newWindow.opener.document.title);
			alert("newWindow.parent.document: "+newWindow.parent.document);			
			alert("window.frame[0].parent:"+window.frames[0].parent.document.title);
			alert("window.frame[0].opener:"+window.frames[0].opener);
		}
	</script>


<frameset rows="50%,50%">
	<frame src="https://www.w3school.com.cn">
		<frameset cols="25%,75%">
			<frame src="https://www.w3school.com.cn">
			<frame src="https://www.w3school.com.cn">
		</frameset>
</frameset>

結果是:

1、

2、

3、

4、

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *