LoginSignup
8
3

More than 5 years have passed since last update.

iframeに関するTips(広告屋さん向け)

Last updated at Posted at 2018-03-27

FriendlyIFrameとかSafeframeとか。
自分で使ってるやつを置いておく。SF系はこれから拡充する。

 scriptを囲んでいるFriendlyIFrameを取得したい

var fif = window.frameElement;

 scriptがiframe(FriendlyIFrameやSafeframe)に囲われているかどうかの判定をしたい

if(window !== parent) {
  // iframe
  if(window.$sf) {
    // safeframe
  } else if(window.frameElement) {
    // friendly-iframe
  } else {
    // other-iframe
  }
} else {
  // not iframe
}

inDapIF は判定に使っていない。稀に設定してない人がいるので……

 リファラーを取得したい

var url;
if (window.$sf) {
  // safeframe
  url = document.referrer;
} else if(window.frameElement){
  // friendly-iframe
  url = window.top.location.href;
} else {
  // other
  url = window.location.href;
}

 複数のFriendlyIFrameに囲われている時に、最も親のFriendlyIFrameにアクセスしたい

var w = window.window;
while(w.parent.frameElement) {
  w = w.parent.window;
}
var fif = w.frameElement;

最近それっぽく作ったやつ。

8
3
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
8
3