简体   繁体   English

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

[英]automatic iframe resize, based on content

I'm not sure why, but my code isn't working... 我不确定为什么,但是我的代码无法正常工作...

Issue: iframe won't resize. 问题:iframe无法调整大小。

I have scoured the web for solutions, and I have identical code on another domain, which works... 我在网上搜寻解决方案,并且在另一个域上有相同的代码,这个代码可以工作...


the code: 编码:

<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>

-------------- The Domain: montanacodeannotated.com --------------域:montanacodeannotated.com

Your page give a Javascript error: 您的页面给出了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.

You cannot access the frame's content(or in this case, the scrollheight of the child page) if the protocols, domain or port don't match. 如果协议,域或端口不匹配,则无法访问框架的内容(在本例中为子页面的滚动高度)。

There is a library on GitHub that claims to work: https://github.com/davidjbradshaw/iframe-resizer GitHub上有一个声称可以正常工作的库: https : //github.com/davidjbradshaw/iframe-resizer

Give it a go. 搏一搏。

This is because the frame has a different origin so you can't access the content within it. 这是因为框架的来源不同,所以您无法访问其中的内容。 you'll see a message like this in your inspector: 您会在检查器中看到以下消息:

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.

Perhaps you could pull down a mirror version for your site? 也许您可以为您的网站下载镜像版本? Running wget -r http://leg.mt.gov/bills/mca_toc/ would recursively copy a version that you could put on your own domain (you might want to ask the origin site first of course!). 运行wget -r http://leg.mt.gov/bills/mca_toc/会递归地复制一个可以放在您自己的域中的版本(当然,您可能要先询问原始站点!)。

Once that was hosted on the same domain, I think the code above would work. 一旦将其托管在同一个域中,我认为上面的代码将起作用。

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

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