Access denied contentWindow iframe IE9
我在www.domain.com上有一个页面,在sub.domain.com上有另一个页面。 sub.domain.com将在iframe中显示在www.domain.com上。 我正在尝试根据iframe的内容动态计算此iframe的高度。
1 2 3 4 5 6 7 8 9 10 11 | <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; } |
src页面确实包含document.domain ='domain.com';
它可以在IE9之外的所有主流浏览器上运行,这使我无法通过上面的javascript访问。 我阅读的所有解决方案都是将document.domain行动态添加到iframe源中,但是如上所述,这已经在我的静态iframe源中。
怎么了?
-编辑-
根据建议,我现在在www.domain.com上找到它
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <script type="text/javascript"> document.domain="domain.com"; <iframe style="border:0px;width:100%;" onload="calcHeight();" id="iframe" src="sub.domain.com"></iframe> // IE hack var ua = window.navigator.userAgent; var msie = ua.indexOf("MSIE"); if (msie > -1) { var source ="javascript:'window.onload=function(){document.write(\\\'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; } |
-编辑2-
根据最后的评论,我现在有这个
1 2 3 4 5 6 7 8 9 10 11 12 13 | <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; } <iframe style="border:0px;width:100%;" onload="calcHeight();" id="iframe" src="javascript:'window.onload=function(){document.write(\'docum??ent.domain=\'domain.com\';<\\\\/script>\');document.close();return \'+url+\';};'"></iframe> |
现在,我不再看到源页面。
抱歉,但是因为我遇到了同样的问题并且以这种方式解决了,我能否请您以这种方式尝试一下:
1 2 3 4 5 6 | var url = $('#iframe').attr("src"); if (isMSIE() == true) { url ="javascript:'window.onload=function(){document.write(\\\'document.domain=\\\\\"" + document.domain +"\\\\\";<\\\\\\\\/script>\\\');document.close();return \\\\\"+url+\\\\\";};<\\/script>'"; } $('#iframe').attr("src", url); |
问题似乎与IE有关,我所做的是用动态javascript替换url,以更改域并返回iframe的url。
主要是,对我的客户而言,发生的事情是iframe出现得太少了,这是我使用这种方法的动机:
此外,来自IE的iframe:
1 | <iframe width="300" class="vytmn-frame" id="iframe" src="javascript:'window.onload=function(){document.write(\'document.domain="localhost";<\\\\/script>\');document.close();return "+url+";};'" frameborder="0" scrolling="yes" horizontalscrolling="no" verticalscrolling="yes"></iframe> |
换句话说,我的问题是我允许使用全尺寸的iframe,因为浏览器显示的iframe本身太少了,因此看不见。
对不起,我希望这次我给了我我所做的一切。
计算高度是没有用的:您无法访问iframe中的文档。 再次抱歉。