LoginSignup
14
13

More than 5 years have passed since last update.

window.openerで親画面が存在するか否かの分岐

Posted at

恒例のIEで思うように動かなかったのでメモします。

チェックしたブラウザ
- IE11
- firefox
- chrome

window.openerの値

親画面が存在する場合

全てのブラウザでオブジェクトが入る

親画面が存在しない場合

firefox、chrome

null

IE

undefined もしくは 空オブジェクト

IEでwindow.openerの値が空オブジェクトになる場面

子画面を出した後、親画面を閉じると、子画面においてwindow.openerの値が空オブジェクトになる

IEにも対応した、window.openerで親画面が存在するか否かの分岐方法

if ( !window.opener || !Object.keys(window.opener).length ) {
  // 親画面が存在しない
} else {
  // 親画面が存在する
}

if ( window.opener || Object.keys(window.opener).length ) {} とすると
Object.keys()にオブジェクト以外を渡してエラーになるので注意です。

一言

相変わらずですねIEさん・・・

14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13