DIV CSS 佈局教程網

window.parent 與 window.opener 到底差在哪
編輯:JavaScript基礎知識     

這兩個屬性名稱相近,用途卻完全不同:一個用於 iframe 框架結構,一個用於彈出視窗。用錯了就會得到 undefined 或跨視窗存取失敗。

一、location 的層級關係

先釐清導覽的層級:

  • window.location.href / location.href:目前頁面跳轉
  • parent.location.href:上一層頁面跳轉
  • top.location.href:最外層頁面跳轉

舉例說明:若 A、B、C、D 都是 jsp 頁面,D 是 C 的 iframe,C 是 B 的 iframe,B 是 A 的 iframe,那麼在 D 中執行:

  • location.href → D 頁面跳轉
  • parent.location.href → C 頁面跳轉
  • top.location.href → A 頁面跳轉

這也說明了連結中 target 屬性的意義:

  • _blank:在新視窗開啟
  • _parent:在父框架中載入
  • _self:在目前框架中載入
  • _top:在最外層視窗中載入,會跳出整個框架集

所以:parent.window 是父視窗物件,top.window 是最外層視窗物件。

二、window.parent:iframe 內存取父頁面

當 iframe 內嵌的頁面需要存取外層頁面時,用 window.parent。

A.html(父頁面):

<html>
<head>
  <title>父頁面</title>
</head>
<body>
  <form id="form1" action="">
    <div>
      輸入值:
      <input type="text" name="username" id="username" /><br />
      <iframe src="b.html" width="400px" height="300px"></iframe>
    </div>
  </form>
</body>
</html>

B.html(子頁面):

<html>
<head>
  <script type="text/javascript">
  function getParentValue() {
    document.getElementById("span1").innerText =
      window.parent.document.getElementById("username").value;
  }
  </script>
</head>
<body>
  <span>文本框值為:</span><span id="span1"></span><br />
  <input type="button" value="獲取父窗口內的文本框值" onclick="getParentValue();" />
</body>
</html>

三、window.opener:彈出視窗存取開啟者

window.opener 則是「用 window.open() 或 target="_blank" 開啟的子頁面」用來指向開啟它的那一頁。

a.html(開啟者):

<html>
<head>
  <title>父頁面</title>
  <script type="text/javascript">
  function openB() {
    window.open('b.html', 'b',
      'width=400,height=200,status=no,toolbar=no,menubar=no,location=no,resizable=yes,left=200,top=100');
  }
  </script>
</head>
<body>
  <form id="form1" action="">
    <div>
      輸入值:
      <input type="text" name="username" id="username" /><br />
      <input type="button" value="打開窗口B" onclick="openB();" /><br />
      <a href="b.html" target="_blank">超鏈接打開B頁面</a>
    </div>
  </form>
</body>
</html>

b.html(被開啟者):

<html>
<head>
  <script type="text/javascript">
  function getOpenerValue() {
    document.getElementById("span1").innerText =
      window.opener.document.getElementById("username").value;
  }
  </script>
</head>
<body>
  <span>文本框值為:</span><span id="span1"></span><br />
  <input type="button" value="獲取父窗口內的文本框值" onclick="getOpenerValue();" />
</body>
</html>

四、常用情境速查

parent / top 系列,通常用於 frameset 或 iframe 結構:

// 登出整個框架後回到登入頁
parent.window.location = 'Login.aspx';
top.window.location = 'Login.aspx';

// 重新載入
window.parent.location.reload();
window.parent.MainForm.location.reload();

// 取得其他框架中的元素值
window.parent.MainForm.form1.text1.value;

opener 系列,用於新視窗與開啟者之間:

window.opener.location.reload();
window.opener.document.getElementById("txtName").value;

指定框架的歷史操作:

top.playFrame.history.go(-1);   // 後退
top.playFrame.history.go(1);    // 前進
top.playFrame.location.reload();// 重新載入

五、實務提醒

這套寫法屬於 iframe 時代的產物。現代開發中,跨視窗溝通更推薦使用 postMessage(),它支援跨來源通訊且有明確的來源驗證機制。

另外要注意同源政策:若 iframe 或彈出視窗與目前頁面不同源(通訊協定、網域、連接埠任一不同),存取對方的 document 會直接拋出安全錯誤。遇到這種情況,唯一正解就是改用 postMessage。

最後一個安全提醒:使用 target="_blank" 時,被開啟的頁面能透過 window.opener 反向操作你的頁面,這是一項已知風險。請在連結上加上 rel="noopener noreferrer",或是在 window.open 後將新視窗的 opener 設為 null。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved