访问被拒绝的内容窗口iframe IE9

时间:2015-04-23 13:40:04

标签: javascript html internet-explorer dom iframe

我在www.domain.com上有一个页面,在sub.domain.com上有另一个页面。 sub.domain.com将在www.domain.com上显示在iframe中。我尝试根据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 =&#39; domain.com&#39;;

除了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>docum‌​ent.domain=\'domain.com\';<\\/script>\');document.close();return \'+url+\';};</script>'"></iframe>

现在我再也看不到源页了。

1 个答案:

答案 0 :(得分: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,改变域并返回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中的文档。对不起。

相关问题