この種の問題が発生しましたが、状況とは少し逆になりました。他のドメインのサイトにiframedコンテンツを提供していたため、同一生成元ポリシーも問題でした。グーグルのトロール網に何時間も費やした後、私たちは最終的に(ある程度..)実行可能な解決策を見つけました。それはあなたのニーズに適応できるかもしれません。
同一生成元ポリシーを回避する方法はありますが、iframedコンテンツとフレーミングページの両方で変更が必要なため、両側で変更をリクエストする機能がない場合、この方法はあまり役に立ちません。私は怖いです。
同じオリジンポリシーを回避できるブラウザの癖があります-javascriptは、独自のドメイン上のページ、またはiframされたページと通信できますが、フレーム化されたページとは通信できません。
www.foo.com/home.html, which iframes
|-> www.bar.net/framed.html, which iframes
|-> www.foo.com/helper.html
その後、 (iframed)および(同じドメイン)とhome.html
通信できます。framed.html
helper.html
Communication options for each page:
+-------------------------+-----------+-------------+-------------+
| | home.html | framed.html | helper.html |
+-------------------------+-----------+-------------+-------------+
| www.foo.com/home.html | N/A | YES | YES |
| www.bar.net/framed.html | NO | N/A | YES |
| www.foo.com/helper.html | YES | YES | N/A |
+-------------------------+-----------+-------------+-------------+
framed.html
helper.html
(iframed)にメッセージを送信できますが、できません home.html
(子は親とクロスドメインで通信できません)。
ここで重要なのは、helper.html
からメッセージを受信できframed.html
、と通信できることですhome.html
。
つまり、基本的に、framed.html
ロードすると、それ自体の高さを計算helper.html
し、メッセージをに渡します。これにより、が置かhome.html
れているiframeのサイズを変更できframed.html
ます。
framed.html
からにメッセージを渡す最も簡単な方法はhelper.html
、URL引数を使用することでした。これを行うにframed.html
は、指定されたiframeがありsrc=''
ます。起動するonload
と、自身の高さを評価し、この時点でのiframeのsrcを次のように設定します。helper.html?height=N
Facebookがそれをどのように処理するかについての説明がここにありますが、これは上記の私のものよりも少し明確かもしれません!
コード
ではwww.foo.com/home.html
、次のjavascriptコードが必要です(ちなみに、これは任意のドメインの.jsファイルからロードできます。):
<script>
// Resize iframe to full height
function resizeIframe(height)
{
// "+60" is a general rule of thumb to allow for differences in
// IE & and FF height reporting, can be adjusted as required..
document.getElementById('frame_name_here').height = parseInt(height)+60;
}
</script>
<iframe id='frame_name_here' src='http://www.bar.net/framed.html'></iframe>
でwww.bar.net/framed.html
:
<body onload="iframeResizePipe()">
<iframe id="helpframe" src='' height='0' width='0' frameborder='0'></iframe>
<script type="text/javascript">
function iframeResizePipe()
{
// What's the page height?
var height = document.body.scrollHeight;
// Going to 'pipe' the data to the parent through the helpframe..
var pipe = document.getElementById('helpframe');
// Cachebuster a precaution here to stop browser caching interfering
pipe.src = 'http://www.foo.com/helper.html?height='+height+'&cacheb='+Math.random();
}
</script>
内容www.foo.com/helper.html
:
<html>
<!--
This page is on the same domain as the parent, so can
communicate with it to order the iframe window resizing
to fit the content
-->
<body onload="parentIframeResize()">
<script>
// Tell the parent iframe what height the iframe needs to be
function parentIframeResize()
{
var height = getParam('height');
// This works as our parent's parent is on our domain..
parent.parent.resizeIframe(height);
}
// Helper function, parse param from request string
function getParam( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
</script>
</body>
</html>