繁体   English   中英

根据内容自动调整iframe的大小

[英]automatic iframe resize, based on content

我不确定为什么,但是我的代码无法正常工作...

问题:iframe无法调整大小。

我在网上搜寻解决方案,并且在另一个域上有相同的代码,这个代码可以工作...


编码:

<html>

<head>

<base target="_self">
<title>Montana Code Annotated - State Law</title>
<meta name="title" content="">
<meta name="description" content="">
<meta name="keywords" content="">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<script type="text/javascript">
<!--
function resizeIframe(id){
/*
this.obj=obj
//this.obj.width=null
//this.obj.width=window.frames['sizeframe'].document.body.scrollWidth
this.obj.style.height='' // for Firefox and Opera
setTimeout('this.obj.style.height=this.obj.contentWindow.document.body.scrollHeight+(notIE?heightOffset:0)',10) // setTimeout required for Opera
*/

el=document.getElementById(id)
el.style.height="200px" // for Firefox and Opera
setTimeout("el.style.height=el.contentWindow.document.body.scrollHeight+'px'",1) // setTimeout required for Opera
}

// -->
</script>

</head>

<body topmargin="0" bottommargin="0"><div align="center">


<p><br>
&nbsp;</p>
<p>
<iframe name="windowoflaw" id="sizeframe" allowTransparency="true" onload="resizeIframe(this.id)" marginwidth="1" marginheight="1" height="200" width="800" scrolling="no" align="center" border="0" frameborder="0" src="http://leg.mt.gov/bills/mca_toc/">
</iframe></p>
</div>

</body>

</html>

--------------域:montanacodeannotated.com

您的页面给出了Javascript错误:

Uncaught SecurityError: Blocked a frame with origin "mytesturl" from accessing a frame with origin "http://leg.mt.gov". Protocols, domains, and ports must match.

如果协议,域或端口不匹配,则无法访问框架的内容(在本例中为子页面的滚动高度)。

GitHub上有一个声称可以正常工作的库: https : //github.com/davidjbradshaw/iframe-resizer

搏一搏。

这是因为框架的来源不同,所以您无法访问其中的内容。 您会在检查器中看到以下消息:

Uncaught SecurityError: Blocked a frame with origin "http://montanacodeannotated.com" from accessing a frame with origin "http://leg.mt.gov". Protocols, domains, and ports must match.

也许您可以为您的网站下载镜像版本? 运行wget -r http://leg.mt.gov/bills/mca_toc/会递归地复制一个可以放在您自己的域中的版本(当然,您可能要先询问原始站点!)。

一旦将其托管在同一个域中,我认为上面的代码将起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM