Bu tür bir sorun yaşadık, ancak durumunuzun biraz tersi - iframed içeriği diğer alanlardaki sitelere sağlıyorduk, bu yüzden aynı menşe politikası da bir sorundu. Google'ı trol etmek için saatler geçirdikten sonra, sonunda ihtiyaçlarınıza göre uyarlayabileceğiniz (biraz ..) uygulanabilir bir çözüm bulduk.
Aynı başlangıç politikasının etrafında bir yol var, ancak hem iframed içerikte hem de çerçeveleme sayfasında değişiklik gerektiriyor, bu nedenle her iki tarafta değişiklik isteme yeteneğiniz yoksa, bu yöntem sizin için çok yararlı olmayacak, korkarım.
Aynı köken politikasını doldurmamıza izin veren bir tarayıcı tuhaflığı var - javascript ya kendi alanındaki sayfalarla ya da iframe yaptığı sayfalarla iletişim kurabilir, ancak çerçevelendiği sayfalarla asla iletişim kuramaz, örneğin:
www.foo.com/home.html, which iframes
|-> www.bar.net/framed.html, which iframes
|-> www.foo.com/helper.html
daha sonra (iframed) ve (aynı alan adı) home.htmlile iletişim kurabilir .framed.htmlhelper.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.htmlhelper.html(iframed) 'e mesaj gönderebilir ancak gönderemez home.html (alt alanlarla üst öğe arasında iletişim kuramaz ).
Burada anahtar olmasıdır helper.htmlmesaj alabilen framed.htmlve ayrıca iletişim kurabilir ile home.html.
Bu nedenle, esasen, framed.htmlyüklendiğinde, kendi yüksekliğini çalıştırır, helper.htmlmesajı home.htmliletir, hangi framed.htmloturduğu iframe'i yeniden boyutlandırabilir .
Biz mesaj iletmeyi bulundu basit yolu framed.htmliçin helper.htmlbir URL argüman yoluyla oldu. Bunu yapmak framed.htmliçin src=''belirtilen bir iframe vardır . onloadAteş ettiğinde kendi yüksekliğini değerlendirir ve iframe'in src'sini bu noktadahelper.html?height=N
Burada facebook'un nasıl işlediğine dair bir açıklama var , bu yukarıdaki benimkinden biraz daha açık olabilir!
kod
İçinde www.foo.com/home.html, aşağıdaki javascript kodu gereklidir (bu, herhangi bir etki alanındaki bir .js dosyasından tesadüfen yüklenebilir ..):
<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>
İçinde 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>
İçeriği 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>