Link to home
Start Free TrialLog in
Avatar of Neoliten
Neoliten

asked on

Iterate through all iframes in the document

I have a html document with code:

<body>
<iframe id="full_content_frame" src="tmpl_content1.htm" frameborder="0" width="100%">
</iframe>
<iframe id="full_content_frame" src="tmpl_content2.htm" frameborder="0" width="100%">
</iframe>
......
</body>


and in tmpl_contentX.html I have:
<body onload="resizeIframe('full_content_frame')">
.....
</body>



and main goal is to determine height of each frame and adjust iframe.height that all frames would be one by one vertically.
I am looking for any possibility to achieve that and one technique was successfull:

function resizeIframe(id)
{
       parent.document.getElementById(id).style.height = (this.document.body.scrollHeight) + "px";
}

But it will correctly adjust only the first frame. How I can apply such method for all frames in parent document?
Or is there another flexible method to adjust numerous ifames?
Avatar of Roonaan
Roonaan
Flag of Netherlands image

First, id's should be unique. You cannot have two tags with the same id.

<script type="text/javascript">
  function doIt() {
    var d = document.getElementsByTagName('iframe');
    for(var i = 0; i < d.length; i++) {
      d[i].style.height = (this.document.body.scrollHeight/d.length) + "px";
    }
  }
</script>
<body>
  <iframe id="full_content_frame" src="tmpl_content1.htm" frameborder="0" width="100%">
  </iframe>
  <iframe id="full_content_frame" src="tmpl_content2.htm" frameborder="0" width="100%">
  </iframe>
  <input type="button" onclick="doIt();" value="doIt();" />
</body>

-r-
Avatar of Neoliten
Neoliten

ASKER

Thanks, but how to do it automatically?
Becides, height of all frames would be the same.
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Check this:

<body>
<iframe id="full_content_frame" src="tmpl_content1.htm" frameborder="0" width="100%">
</iframe>
<iframe id="full_content_frame" src="tmpl_content2.htm" frameborder="0" width="100%">
</iframe>
<iframe id="full_content_frame" src="tmpl_content3.htm" frameborder="0" width="100%">
</iframe>
<iframe id="full_content_frame" src="tmpl_content4.htm" frameborder="0" width="100%">
</iframe>
<script>
var frm = window.frames;
var pct = parseInt(1/frm.length*100)+"%";
alert(pct)
for(var i=0;i<frm.length;i++){
  frm[i].frameElement.style.height=pct;
}
</script>
</body>