У меня есть страница на www.domain.com и другая страница на sub.domain.com. sub.domain.com будет отображаться на www.domain.com в iframe. Я пытаюсь динамически вычислять высоту этого iframe на основе содержимого iframe.Доступ запрещен contentWindow iframe IE9
<iframe onload="calcHeight();" id="iframe" src="sub.domain.com"></iframe>
<script type="text/javascript">
function calcHeight()
{
//find the height of the internal page
document.domain = 'domain.com';
var the_height=parent.document.getElementById('iframe').contentWindow.document.body.scrollHeight +25;
//change the height of the iframe
parent.document.getElementById('iframe').height=the_height;
}
</script>
Сайт src содержит документ.domain = 'domain.com';
Он работает над всеми основными браузерами, кроме IE9, что дает мне доступ, запрещенный на javascript выше. Все прочитанные мной решения динамически добавляют строку document.domain в источник iframe, но, как сказано, это уже в моем статическом источнике iframe.
Неправильно?
-edit-
Основываясь на предположении, теперь у меня есть это на www.domain.com
<script type="text/javascript">
document.domain="domain.com";
</script>
<iframe style="border:0px;width:100%;" onload="calcHeight();" id="iframe" src="sub.domain.com"></iframe>
<script>
// IE hack
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE");
if (msie > -1) {
var source = "javascript:'<script>window.onload=function(){document.write(\\'<script>document.domain=\\\"" + document.domain + "\\\";<\\\\/script>\\');document.close();return \\\"+url+\\\";};<\/script>'";
$('#iframe').attr("src", source);
}
// End IE hack
function calcHeight()
{
//find the height of the internal page
var the_height=parent.document.getElementById('iframe').contentWindow.document.body.scrollHeight +25;
//change the height of the iframe
parent.document.getElementById('iframe').height=the_height;
}
</script>
-edit 2- на основе последних замечаний я теперь этот
<script type="text/javascript">
document.domain="domain.com";
url="sub.domain.com";
function calcHeight()
{
//find the height of the internal page
var the_height=parent.document.getElementById('iframe').contentWindow.document.body.scrollHeight +25;
//change the height of the iframe
parent.document.getElementById('iframe').height=the_height;
}
</script>
<iframe style="border:0px;width:100%;" onload="calcHeight();" id="iframe" src="javascript:'<script>window.onload=function(){document.write(\'<script>document.domain=\'domain.com\';<\\/script>\');document.close();return \'+url+\';};</script>'"></iframe>
Теперь я больше не вижу исходную страницу.
У меня нет понятия, что вы имеете в виду со вторым замечанием? –