【问题标题】:Access denied contentWindow iframe IE9访问被拒绝的 contentWindow iframe IE9
【发布时间】: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>docum‌​ent.domain=\'domain.com\';<\\/script>\');document.close();return \'+url+\';};</script>'"></iframe>

现在我看不到源页面了。

【问题讨论】:

  • 我不知道你说的第二句话是什么意思?

标签: javascript html internet-explorer dom iframe


【解决方案1】:

对不起,但是因为我遇到了同样的问题并且我以这种方式解决了,所以我可以要求您以这种方式尝试一下吗:

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 有关,我所做的是用动态 javascript 替换 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=\&quot;localhost\&quot;;<\\/script>\');document.close();return \&quot;+url+\&quot;;};</script>'" frameborder="0" scrolling="yes" horizontalscrolling="no" verticalscrolling="yes"></iframe>

换句话说,我的问题是我允许一个全尺寸的 iframe,因为 iframe 本身被浏览器显示得太少而且不可见。

对不起,我希望这一次我给了你我所做的一切。 计算高度是没有用的:您无法访问 iframe 内的文档。再次抱歉。

【讨论】:

  • 那么,我应该在上面几行中添加这个?
  • 在您拥有 iframe 的首页中,因此如果在 IE 下更改 url 属性。我上周刚刚解决了一个类似的问题。就像你可以注意到我设置了 document.domain 并返回 iframe url。我只要求你试一试。希望你能像我一样解决问题。
猜你喜欢
  • 2014-06-20
  • 1970-01-01
  • 2014-11-20
  • 2016-08-13
  • 1970-01-01
  • 2013-02-06
  • 2012-05-12
  • 1970-01-01
  • 2012-05-15
相关资源
最近更新 更多