views:

55

answers:

2

I have an iframe which content is a php-file, and that php-file displays some mysql records.

The more records, the larger the iframe height should get, otherwise it wont show all of the information in the iframe.

Here is a code I have for dynamically setting the iframe height, from the iframes content (the php file):

var x = document.body.scrollHeight;
x = x + 20 + 'px';
window.parent.document.getElementById("iframe001").style.height=x;

iframe001 is the iframe I am referring to above. I am adding 20px of height extra because of borders etc...

Now, sometimes this resizing works, sometimes it doesn't.

Does anybody know why it works sometimes. If I alert the 'x' variable, it shows different values sometimes, but most times it works.

Any other ways you know of setting the iframes height from the content of the iframe?

Thanks

A: 

just taking a shot; maybe you're experiencing casting errors? might be safer to do;

var x = document.body.scrollHeight+20;
x = x + 'px';
window.parent.document.getElementById("iframe001").style.height=x;

if that doesn't help; what does the x-variable look like when resizing is not working?

futtta
No, it doesn't work. The x alerted shows different values, it isn't always the same val...
Camran
1+2+'px' evals to '3px'
Alsciende
+1  A: 

do you run this piece of javascript in any kind of onload-event? if not, maybe the scrollHeight isn't kown when the script is executed.

oezi
no i dont, I run it at the end of the php-file which is run inside the iframe
Camran
so, could you please put this three lines of code into a function, and call this function in the onload-handler of your iframe-content?
oezi