我在 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>
现在我再也看不到源页面了。
最佳答案
抱歉,但是因为我遇到了同样的问题并且我以这种方式解决了,所以我可以请你完全按照这种方式尝试一下吗:
var url = $('#iframe').attr("src");
if (isMSIE() == true) {
url = "javascript:'<script>window.onload=function(){document.write(\\'<script>document.domain=\\\"" + document.domain + "\\\";<\\\\/script>\\');document.close();return \\\"+url+\\\";};<\/script>'";
}
$('#iframe').attr("src", url);
问题似乎与 IE 有关,我所做的是将 url 替换为动态 javascript,更改域并返回 iframe 的 url 主要是,我的客户遇到的情况是 iframe 出现得太少,这就是我使用这种方法的动机:
此外,来自 IE 源的 iframe:
<iframe width="300" class="vytmn-frame" id="iframe" src="javascript:'<script>window.onload=function(){document.write(\'<script>document.domain=\"localhost\";<\\/script>\');document.close();return \"+url+\";};</script>'" frameborder="0" scrolling="yes" horizontalscrolling="no" verticalscrolling="yes"></iframe>
换句话说,我的问题是我允许使用全尺寸的 iframe,因为浏览器显示的 iframe 本身太少并且不可见。
抱歉,我希望这次我给了你我所做的。 计算高度是没有用的:您无法访问 iframe 内的文档。再次抱歉。
关于javascript - 访问被拒绝 contentWindow iframe IE9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29825229/