user2961151
user2961151

Reputation: 1

automatic iframe resize, based on content

I'm not sure why, but my code isn't working...

Issue: iframe won't resize.

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

Upvotes: 0

Views: 1216

Answers (2)

Milanzor
Milanzor

Reputation: 1930

Your page give a Javascript error:

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

Give it a go.

Upvotes: 1

benjaminbenben
benjaminbenben

Reputation: 1228

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!).

Once that was hosted on the same domain, I think the code above would work.

Upvotes: 1

Related Questions