动态iframe高度取决于PHP内容?

时间:2009-11-29 09:56:25

标签: php html

如何将我的Iframe高度设置为动态内容。

内容只是PHP代码,没有别的。

我使用php查询数据库,然后显示一些广告。广告越多,iframe就越高。

我认为身高='100%'应该这样做,但没有......

我已阅读其他问题,但他们的解决方案对我不起作用。

建议?

由于

2 个答案:

答案 0 :(得分:1)

你必须在javascript中设置iframe的高度。 JavaScript应位于iframe内的页面中(必须是if if页面,iframe中的页面来自不同的域)。

a.html:

<iframe src="b.html" id="myiframe"></iframe>

b.html:

<div style="height: 500px; background:magenta;"></div>
<script>
    parent.document.getElementById('myiframe').style.height = document.body.offsetHeight+'px';
</script>

答案 1 :(得分:0)

如果广告只是文字,您可以使用line height * number of lines作为高度。我假设你知道字体大小,可以通过计算<br />来找到行数:

<?php
// $content is your ads
// assuming ads.php returns an ad with an id, this example uses ad #10
// We are reading it with a full url because we don't want to read it as just
// a text file (we want it to be interpreted by php before we load it)
$content = file_get_contents('http://www.example.com/ads.php?id=10');
$line_height = 12; // line-height: 12px; is set in CSS
$number_of_lines = substr_count($content,'<br />'); // you can search for <br>
                                                    // and <br/> as well if you 
                                                    // need to
$height = $line_height * $number_of_lines; // You may also want to have padding

?>
<iframe src="ads.php?id=10" height="<?php echo $height; ?>" />

编辑:将$ font_size更改为$ line_height