Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取iframe的高度_Javascript_Jquery_Html_Iframe - Fatal编程技术网

Javascript 获取iframe的高度

Javascript 获取iframe的高度,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,我正在构建一个对话框,其内容是iframe。当对话框打开时,我有以下代码,这些代码是我从各种来源提取的,以使其工作: var iframeDOM = document.createElement("IFRAME"); iframeDOM.setAttribute("frameborder", "0"); iframeDOM.setAttribute("scrolling", plugin.settings.scrolling ? "" : "no"); iframeDOM.setAttri

我正在构建一个对话框,其内容是iframe。当对话框打开时,我有以下代码,这些代码是我从各种来源提取的,以使其工作:

var iframeDOM = document.createElement("IFRAME"); 
iframeDOM.setAttribute("frameborder", "0"); 
iframeDOM.setAttribute("scrolling", plugin.settings.scrolling ? "" : "no"); 
iframeDOM.setAttribute("allowtransparency", isIE ? "true" : ""); 



var setDimensions = function(){
                        console.log("load");
                        var iframeWin = iframeDOM.contentWindow || iframeDOM.contentDocument.parentWindow;
                        if (iframeWin.document.body) {
                            console.log("new height: " + iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight); //Not giving real height, always 0
                            console.log("new client height: " + iframeWin.document.documentElement.clientHeight); 
                            iframeDOM.height = 550;
                            iframeDOM.width  = 550;
                        }

                    };

                    $(iframeDOM).load(setDimensions)
                                .appendTo($element.children().first())
                                .attr({ src: plugin.settings.href, name: new Date().getTime() });  
如您所见,我试图记录iFrame的高度,但它总是打印0(作为临时解决方法,我静态设置尺寸)

编辑:我更新此OP的另一个解决方案返回错误:

 Error: Permission denied to access property 'document' if (iframeWin.document.body) {

Iframe不会自动获取高度和宽度。您需要手动定义它,因此只有通过javascript才能捕获加载到iframe中的页面的实际高度,并将其指定为iframe的高度。但这种做法会造成不稳定

更新


使用.height()函数计算页面高度,并将其分配给iframe。它将以这种方式工作。

我发现有一种方法非常有效。我认为它需要调整,但我不擅长JavaScript

<script language="JavaScript">
<!--
function autoResize(id){
  var newheight;
  var newwidth;

if(document.getElementById){
    newheight=document.getElementById(id).contentWindow.document.body.scrollHeight;
    newwidth=document.getElementById(id).contentWindow.document.body.scrollWidth;
}

document.getElementById(id).height= (newheight) + "px";
document.getElementById(id).width= (newwidth) + "px";
}
//-->
</script>

<iframe name="content" src="http://my-domain.com/home.html" width="100%" height="200px" id="iframe1" marginheight="0" frameborder="0" onLoad="autoResize('iframe1');">
</iframe>


也许能帮上忙,我刚刚尝试了clientHeight,并将其添加到OP中,还返回了0:(同时检查此链接@FaceOfJock,以了解我得到的答案错误:访问属性“document”if(iframeWin.document.body)的权限被拒绝{…我更新了OPI。正如你所看到的,我是通过javascript来完成的。为什么你说它会造成不稳定?它会先加载带有默认高度和宽度值的iframe,然后加载页面并计算页面的高度,然后再分配回iframe,这将导致iframe跳舞。没关系,它一开始是隐藏的我在分配尺寸后显示它。不幸的是,我没有看到另一种方式,因为它目前正在加载到一个很小的区域并剪切其余的部分。