以使iframe适合其内容高度 [英] to fit iframe to its content height

查看:67
本文介绍了以使iframe适合其内容高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < script language =JavaScript> 
函数autoResize(id){
alert('check');
var newheight;
var newwidth;

if(document.getElementById){
newheight = document.getElementById(id).contentWindow.document .body.scrollHeight;
newwidth = document.getElementById(id).contentWindow.document .body.scrollWidth;
}
alert(newheight);
alert(newwidth);
document.getElementById(id).height =(newheight)+px;
document.getElementById(id).width =(newwidth)+px;
}
< / script>

HTML:

 < iframe id =faq_iframesrc =http://www.google.comwidth =100%height =200pxscrolling =noframeborder =0marginheight =0onload =javascript:autoResize('faq_iframe');>< / iframe> 

问题:我使用iframe来显示我的页面。我需要适合iframe到我的页面高度,我怎么能做到这一点。



我曾尝试过上面的代码

我通过firebug得到了这个错误

 权限被拒绝用于< http:// localhost>从< http://www.google.co.in>获取属性Window.document。 
[解决此错误] newheight = document.getEleme ... tWindow.document.body.scrollHeight;

如何让iframe适合其页面高度iframe src

解决方案

在你的情况下,你不能。



如果内容位于一个域中的iframe不可能读取包含宽度和高度的属性,包括文档中注入的属性,包括宽度和高度住在另一个域名上。



那里是通过iframe实现跨域通信的几种方式,但它涉及到iframe所在的文档是一个愿意参与的人,并且使用主动javascript来促进它。


        <script language="JavaScript">
        function autoResize(id){
            alert('check');
            var newheight;
            var newwidth;

            if(document.getElementById){
                newheight=document.getElementById(id).contentWindow.document .body.scrollHeight;
                newwidth=document.getElementById(id).contentWindow.document .body.scrollWidth;
            }
            alert(newheight);
            alert(newwidth);
            document.getElementById(id).height= (newheight) + "px";
            document.getElementById(id).width= (newwidth) + "px";
        }
        </script>

HTML:

            <iframe id="faq_iframe" src="http://www.google.com" width="100%" height="200px" scrolling="no" frameborder="0" marginheight="0" onload="javascript: autoResize('faq_iframe');"></iframe>

Question: I am using iframe to display my page. i need to fit iframe to my page height how can i do this.

I had tried above codes

i got this error via firebug

Permission denied for <http://localhost> to get property Window.document from <http://www.google.co.in>.
[Break On This Error] newheight=document.getEleme...tWindow.document.body.scrollHeight; 

How to make iframe to fit its height to height of my page given in iframe src

解决方案

In your case you can't.

It is not possible for an iframe with content that lives on one domain, to read any properties, including width and height, on the document it is injected into if this document lives on another domain.

There are several ways to achieve cross domain communication across iframes but it involves the document that the iframe lives in is a willing participant, with active javascript to facilitate it.

这篇关于以使iframe适合其内容高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆