Window:opener 屬性
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Window
介面的 opener
屬性會回傳開啟目前視窗的視窗參考,開啟方式可能是透過 open()
,或是導覽具有 target
屬性的連結。
換句話說,如果視窗 A
開啟了視窗 B
,B.opener
會回傳 A
。
值
一個類 Window
物件,參考了開啟目前視窗的視窗(使用 window.open()
,或透過設定了 target
屬性的連結)。如果此視窗不是由另一個視窗連結或建立的,則回傳 null
。
如果開啟者與目前頁面不同源,則 opener 物件的功能會受到限制。例如,無法存取視窗物件上的變數和函式。然而,可以導覽開啟者視窗,這表示被開啟的頁面可以在原始分頁或視窗中開啟一個 URL。在某些情況下,這可能導致網路釣魚攻擊,其中新開啟的頁面會將原始視窗中開啟的受信任頁面替換為釣魚頁面。
確切來說,對於跨來源的 opener 物件,可以使用以下屬性:
window
self
location
:只有Location.replace
和Location.href
屬性可用close
closed
focus
blur
frames
length
top
opener
parent
postMessage
window[0]
、window[1]
等。
此外,還有一些屬性:then
、[Symbol.toStringTag]
、[Symbol.hasInstance]
、[Symbol.isConcatSpreadable]
,這些屬性被各種 JavaScript 操作使用。這些屬性的值為 undefined
。存取所有其他屬性時會產生 SecurityError
DOMException
。
在以下情況下,瀏覽器不會填入 window.opener
,而是將其保留為 null
:
- 可以透過在連結上指定
rel=noopener
,或在windowFeatures
參數中傳遞noopener
來省略 opener。 - 因具有
_blank
的target
的連結而開啟的視窗不會獲得opener
,除非使用rel=opener
明確要求。 - 具有值為
same-origin
的Cross-Origin-Opener-Policy
標頭會阻止設定opener
。由於新視窗是在不同的瀏覽情境中載入,因此它不會有對開啟視窗的參考。
規範
Specification |
---|
HTML # dom-opener-dev |