【发布时间】:2015-07-01 17:51:16
【问题描述】:
我在 www.domain.com 上有一个页面,在 sub.domain.com 上有另一个页面。 sub.domain.com 将显示在 iframe 中的 www.domain.com 上。我正在尝试根据 iframe 的内容动态计算此 iframe 的高度。
<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 页面包含 document.domain= 'domain.com';
它适用于除 IE9 之外的所有主要浏览器,这使我在上面的 javascript 上拒绝访问。我阅读的所有解决方案都是将 document.domain 行动态添加到 iframe 源中,但如前所述,这已经在我的静态 iframe 源中。
怎么了?
-编辑-
根据我现在在 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>
-编辑 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>
现在我看不到源页面了。
【问题讨论】:
-
我不知道你说的第二句话是什么意思?
标签: javascript html internet-explorer dom iframe