Jump to content

Dynamic Iframe Height?


cclloyd9785

Recommended Posts

For this case, you probably shouldn't be using an iframe. Try loading the content using a server-side language. There's only one way to dynamically adjust iframe height, and it's using JAvascript to detect the height of the document within it and resizing the frame. This will only work if the page is in the same domain.

Link to comment
Share on other sites

If you give the iframe an ID attribute, you can access it and its content:

<iframe id="myFrame" src=""></iframe> <script type="text/javascript">var f = document.getElementById("myFrame");f.onload = function() {  this.height = this.contentWindow.document.body.offsetHeight;}</script>

Server-side languages need to be supported by the server that your document is hosted.W3Schools has a PHP tutorial. What you want to research is includes. Don't make the mistake of including an entire HTML document, only put the specific lines of code that you need in the file.

Link to comment
Share on other sites

PHP seems a lot more complicated. But I'm pretty sure my host supports php5. The JS code didn't seem to have worked.

      <iframe id="summons" src="/summons.html" scrolling="no" style="width:602px; border:0px; height:auto">Update your browser.</iframe>   <script type="text/javascript">var f = document.getElementById("summons");f.onload = function() {this.height = this.contentWindow.document.body.offsetHeight;} </script></div>

Link to comment
Share on other sites

You're setting the height to "auto" with CSS, which is probably overriding the height attribute of the frame. Since you're doing that, you'll have to change the code to manipulate the CSS instead.

this.style.height = this.contentWindow.document.body.offsetHeight + "px"; 

Link to comment
Share on other sites

It looks like I'd have to do some debugging. We have to see that the onload event is firing, and then see whether this.contentWindow.document.body.offsetHeight has a value. You can do both those things with this code:

<script type="text/javascript"> var f = document.getElementById("myFrame");f.onload = function() {  alert(this.contentWindow.document.body.offsetHeight);}</script>

If a dialog appears, then the function is being called.

Link to comment
Share on other sites

Why do you have an iframe there? Can't you just put the table right into there itself? The onload event isn't firing, probably because the page inside the frame finished loading before the script executed. Since the content of the iframe never changes you probably can omit the event:

<script type="text/javascript"> var f = document.getElementById("myFrame"); f.height = f.contentWindow.document.body.offsetHeight; </script>

Link to comment
Share on other sites

It looks like you certainly need PHP. With PHP you could develop a system that lets you add or remove items from the database. You wouldn't need to edit the HTML at all. Again, PHP includes are really simple. Just include a file like this:

<?php include 'file.html'; ?>

Save the page with a .php extension.

Link to comment
Share on other sites

you would place a single include file in the place you wish that specific code to appear

<div id="wrapper"><?php include("header.html"); ?><?php include("nav.php"); ?><div id="content"><?php include("summons.html"); ?></div><?php include("footer.php"); ?></div>

if the include page contains php code, as in <?php echo "hello world"; ?> it have to be saved as a php file, but it can use a combination of both html and php, as in <h1><?php echo "hello world";?></h1>.

Link to comment
Share on other sites

Using PHP worked, but now it is using the default color for links. I have a class on all of the a that make them grey/white. The default with no class is the red it currently shows. Also quick question. When including HTML into the page with PHP, do I need the head info for it still, or can the html page have just the table, with no header info?

Link to comment
Share on other sites

You should not have any <!DOCTYPE>, <html>, <head>, or <body> tags in the included file. Only have the specific content you have placed there. It just cets copied and pasted. Scripts and style rules of the main page apply to included files.

Link to comment
Share on other sites

It seems to mess up stuff when I include it in with PHP. Edit: Fixed various issues, but now it won't find the files it needs to. I get these errors

[b]Warning[/b]: include() [[url="http://cclloyd.zxq.net/function.include"]function.include[/url]]: Unable to access /inventory/summons.html in [b]/www/zxq.net/c/c/l/cclloyd/htdocs/inventory.php[/b] on line [b]34[/b][b]Warning[/b]: include(/inventory/summons.html) [[url="http://cclloyd.zxq.net/function.include"]function.include[/url]]: failed to open stream: No such file or directory in[b]/www/zxq.net/c/c/l/cclloyd/htdocs/inventory.php[/b] on line [b]34[/b][b]Warning[/b]: include() [[url="http://cclloyd.zxq.net/function.include"]function.include[/url]]: Failed opening '/inventory/summons.html' for inclusion (include_path='.:/usr/lib/php') in[b]/www/zxq.net/c/c/l/cclloyd/htdocs/inventory.php[/b] on line [b]34

[/b] When on line 34 is

[/b]<?php include("/inventory/summons.html"); ?>

And I stored that in that spot, and you can even see so... http://cclloyd.zxq.net/inventory/summons.html

Link to comment
Share on other sites

The include path is relative to the server, so / is going to be pointing to a location way outside the root of your site. Don't use HTTP paths either. Use a relative path, such as "inventory/summons.html"

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...