cross browser iframe auto resixe script/solution

*Please don't submit a googled cut and snip solution. Please only submit one that you have tested and it works*

Need a tried and true cross browser Iframe auto resize script. Googled solutions so far have not worked.

My IFrame:

<iframe name="bodyframe" src="../site_flash/privacypolicyu.htm" height="680" width="758" SCROLLING=NO frameborder="0"  >
WanderinglazyeyeAsked:
Who is Participating?
 
sh0eConnect With a Mentor Commented:
Ya, I made another goof.  This time for real.  I commented it and everything to make sure I was using the working version.  I really need a better editor.

This one will work.
<script type="text/javascript">
function resizeIframe(i) {
    if(i.Document) //IE
		i.style.height = i.Document.body.scrollHeight;
	else if(i.contentWindow){
		if(i.contentWindow.innerHeight && i.contentWindow.scrollMaxY) //FF
			i.height = i.contentWindow.scrollMaxY + i.contentWindow.innerHeight;
		else if(i.contentWindow.document.height) //WebKit
			i.height = i.contentWindow.document.height;	
		else //all else fails
			i.height = i.contentWindow.document.body.scrollHeight;
	}
}
</script>
<iframe src="test.html" onload="resizeIframe(this)" scrolling="no"></iframe>

Open in new window

0
 
sh0eCommented:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23826632.html
I just rewrote part of a script for someone else here trying to do the same thing.

Anyhow, this is how I would code it.  Tested on WebKit, IE6, FireFox 3.
<iframe name="bodyframe" src="../site_flash/privacypolicyu.htm" height="680" width="758" SCROLLING=NO frameborder="0"  onload="resizeIframe(this)"> 
<script type="text/javascript">
function resizeIframe(name) {
      i=name;
      ih = i.document ? i.Document.body.scrollHeight : i.contentDocument.body.scrollHeight;
      i.style.height = ih + 5 + "px";
}
</script>

Open in new window

0
 
sh0eCommented:
Sorry, I pasted the wrong code.  My editor has been glitching on me a lot lately.
<script type="text/javascript">
function resizeIframe(name) {
      i=name;
      ih = i.document ? i.Document.body.scrollHeight : i.contentDocument.body.scrollHeight;
      i.style.height = ih + 5 + "px";
}
</script>
 
 
<iframe name="bodyframe" src="../site_flash/privacypolicyu.htm" height="680" width="758" SCROLLING=NO frameborder="0"  onload="resizeIframe(this)"> 

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
WanderinglazyeyeAuthor Commented:
The firefox browser isn't displaying the iframe.

I put the script in the head section and the iframe line in the body. I am getting an ivalid markup for that line in Dreamweaver??

Says marked invalid becuase its overlapping or unclosed...
<iframe name="bodyframe" src="../site_flash/index_i.htm" height="680" width="758" SCROLLING=NO frameborder="0"  onload="resizeIframe(this)">  

Open in new window

0
 
sh0eCommented:

<script type="text/javascript">
function resizeIframe(name) {
      i=name;
      ih = i.document ? i.Document.body.scrollHeight : i.contentDocument.body.scrollHeight;
      i.style.height = ih + 5 + "px";
}
</script>
 
 
<iframe name="bodyframe" src="../site_flash/privacypolicyu.htm" height="680" width="758" SCROLLING=NO frameborder="0"  onload="resizeIframe(this)"></iframe>

Open in new window

0
 
WanderinglazyeyeAuthor Commented:
Hi Shoe,

Its still cutting off part of the webpage in the IFrame. Its not sizing it for the whole page...

By specifying the height and width in the Iframe is this messing up the script?
0
 
sh0eCommented:
No, it should have no effect on the script.
I have tested the script without problem.  Is it possible for you to show me a demo site for which you are having trouble?
0
 
WanderinglazyeyeAuthor Commented:
I am in the process of setting up the testing account. It will be within 24 hours. In the mean time is there any other way you can evaluate this? The source code is being written in DW. I can post or send the file, or we can wait until the test site is up - either.
0
 
WanderinglazyeyeAuthor Commented:
Here it is:

www.xf-fps.com/index.html

Please ignore the 404 error below.
0
 
WanderinglazyeyeAuthor Commented:
See,its cutoff at the bottom...
0
 
sh0eCommented:
<iframe name="bodyframe" src="../privacypolicyu.htm" height="680" width="758" scrolling="no" frameborder="0" onload="resizeIframe(this)" ></iframe>

The page has a typo, missing the closing quote.
0
 
WanderinglazyeyeAuthor Commented:
Still not functioning correctly. Check the code at the site again...should I substitute name in the script with the I frame name?

www.xf-fps.com/index.html (updated)
0
 
sh0eCommented:
Apparently, the FF part I wrote wasn't working right.
May break when browser developers feel like messing with DOM names again.

<script type="text/javascript">
function resizeIframe(name) {
      i=name;
      ih = i.document ? i.document.body.scrollHeight : i.contentDocument.body.innerHeight + i.contentWindow.scrollMaxY;
      i.style.height = ih + 5 + "px";
}
</script>
 
<iframe name="bodyframe" src="privacypolicyu.htm" height="680" width="758" SCROLLING=NO frameborder="0"  onload="resizeIframe(this)"> 

Open in new window

0
 
WanderinglazyeyeAuthor Commented:
Well at least the page is displaying, but in firefox the iframe is only 1/2 the size of the real page size. So is it possible that IFrames only fo down so far anyway? The web page the Iframe is loading really is like 2 web pages long.
0
 
WanderinglazyeyeAuthor Commented:
Its still constrained to the heigth width dimensions in the iframe statement. If you put the words "test" under the iframe, it never leaves the same position. The size of the iframe is always the same.
0
 
sh0eCommented:
I didn't realize I was testing two separate versions of my code in FF and IE, one that worked in each browser.
So I ended up posting different code that only work for one browser.
So I just junked the whole thing and rewrote it again verbosely.
0
 
WanderinglazyeyeAuthor Commented:
In FF--> the Iframe goes the whole length of the page (yeah!) BUT its cutoff at 1/2 its width

in IE-->doesn't work at all

0
 
WanderinglazyeyeAuthor Commented:
!! Update, I manually set the width and now it works *great*in FF - no prob.

However in IE, doesn't work.
0
 
sh0eConnect With a Mentor Commented:
Sigh.  IE7 right?
Add this to resizeIframe().  The style height: 25px is confusing IE7.
if(i.Document) aa.contentWindow.document.getElementById('content3').parentNode.style.height="auto";

Open in new window

0
 
WanderinglazyeyeAuthor Commented:
Well, not at least IE shows something and FF is working correctly.

But IE is still not rolling it all the way out.

But that begs a bigger question, whether to use a resizing script at all. It seems its prone to malfunction and a browser upgrade may throw it into oblivion for your site. I am inclined to just fix the iframe height at a a standard at this point just out of concern for future functionality. What say you?

0
 
sh0eConnect With a Mentor Commented:
I would recommend you do that, or at least until a standard is agreed on for scrollHeight and company.
On the upside IE8 appears to work with this code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.