Firefox コード例:一番親のウィンドウを取得する(サイドバーからでも有効)
Firefoxアドオンにて、一番親のウィンドウを取得するJavascriptコード例を挙げる。
サイドバーからでも有効に動作する。
以下のようにwindow.topで取得する方がスマートに見えるが、Chromeを超えた場合に狙いと違うものが取れることがあるので注意が必要である。
以下、リファレンスの引用文。
サイドバーからでも有効に動作する。
/***********************************************
一番親のウィンドウを取得する(サイドバーからでも有効)
*/
var _win = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindow);
//オープナーを上へ上へとたどってゆく
while (_win.opener) _win = _win.opener;
//ルートウィンドウにフォーカス
_win.focus();
以下のようにwindow.topで取得する方がスマートに見えるが、Chromeを超えた場合に狙いと違うものが取れることがあるので注意が必要である。
_win = _win.top
以下、リファレンスの引用文。
Accessor for the root of this hierarchy of windows. This root may be the window itself if there is no parent, or if the parent is of different type (i.e. this does not cross chrome-content boundaries).
(XULPlanet: Object Reference: ChromeWindow#topより引用)
スポンサーサイト