2011-03-11 12 views
6

iframe内のページがiframe内にあるかどうかを知るために、多くのウィンドウプロパティを試しました。私は試しました:ページがGoogle Chrome用のIframeにあるかどうかを確認します

if(top.location!= self.location) //doesn't work in Google Chrome 
    alert("I am in an iframe!") 

これは機能しません(Chrome以外のすべてのブラウザで動作します)。 FirefoxとChrome用のユーザースクリプトを作成していますが、Chromeは実際には動作しません。そのページがiframeにあるかどうかをChromeが検出できるかどうかを判断する方法はありますか?これは、フレームのために働く

+0

http://jsfiddle.net/hpVec/これは、Chromeで正常に動作しているようです。 –

+0

var in_iframe =(window.location!= window.parent.location); –

答えて

15

私はそれを引き受けるものiFrame

if (top === self) { 
    // no frame 
} else { 
    //frame 
} 
+0

これは素晴らしい(それは動作する)!ありがとう – user654628

+0

注:これはie8では機能しません – iturgeon

0

window.frameElementも、最も古代のブラウザでサポートされて動作します。現在のウィンドウが埋め込まれているiframe、埋め込み、またはオブジェクトを識別します。

if(!!window.frameElement){ 
    //code to be executed if we are in an iframe 
} 

More here.

関連する問題