Jump to content

Recommended Posts

am using iframe to show dynamic data gained from sql query. So i get to little problem. Height of content is not always same. Is there any way where iframe can change height based on iframe content?

Yes, but it's difficult. As sunfighter pointed out, a DIV and some AJAX would very likely be better.

Link to comment
https://forums.phpfreaks.com/topic/252306-iframe-height/#findComment-1293641
Share on other sites

Is there any way where iframe can change height based on iframe content?

No, there isn't. The iframe itself belongs to the main page, and the filling of the iframe is the 'imported' page. You cannot have the contents of an imported page control an element on the main page.

 

Link to comment
https://forums.phpfreaks.com/topic/252306-iframe-height/#findComment-1295454
Share on other sites

If the iframe and the page in the same domain, you can create a function in the main window to set the iframe height

e.g.

function set_iframe_height(num){
document.getElementById('my_iframe_id').style.height = num+'px';
}

In your iframe page, you'll create an onload event that check the height and call the parent page function

e.g.

function onload_iframe_height(){
var h = Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight)
    );
window.parent.set_iframe_height(h);
}
....
<body onload="onload_iframe_height"
...

not tested

Link to comment
https://forums.phpfreaks.com/topic/252306-iframe-height/#findComment-1295647
Share on other sites

function set_iframe_height(num){document.getElementById('my_iframe_id').style.height = num+'px';}

Put that function in your main page (the page that host the iframe) and call it from the iframe using window.parent.set_iframe_height

 

Both pages need to be in the same domain

Link to comment
https://forums.phpfreaks.com/topic/252306-iframe-height/#findComment-1295858
Share on other sites

How can you influence the iframe heigth in a parent page that is already loaded and rendered by a value that must come from the child page?

Same way you'd influence anything after the page is already loaded and rendered: with JavaScript. The parent and child can access each other if they're on the same domain, so the parent could grab the height of the child content and resize the frame accordingly.

Link to comment
https://forums.phpfreaks.com/topic/252306-iframe-height/#findComment-1295903
Share on other sites

Same way you'd influence anything after the page is already loaded and rendered: with JavaScript. The parent and child can access each other if they're on the same domain, so the parent could grab the height of the child content and resize the frame accordingly.

But what triggers the execution of the javascript on the parent page then, if that is already loaded and rendered?

Link to comment
https://forums.phpfreaks.com/topic/252306-iframe-height/#findComment-1296051
Share on other sites

With these two codes, being the parent and the child file, respectively, it doesn't work:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Iframe-parent.html</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
div#parentDiv {
    width: 500px;
    height: 500px;
    border: 1px solid black;
}
</style>
<script type="text/javascript">
/*<![CDATA[*/
function set_iframe_height(num) {
    document.getElementById('myIframe').style.height = num+'px';
}
/*]]>*/
</script>
</head>
<body>
<div id="parentDiv">
    <iframe id="myIframe" style="width:450px;" src="Iframe-child.html"></iframe>
</div>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Iframe-child.html</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
div#childDiv {
    width: 400px;
    height: 400px;
    border: 1px solid black;
}
</style>
<script type="text/javascript">
/*<![CDATA[*/
function onload_iframe_height() {
var h = Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight)
    );
// var h = 400;
window.parent.set_iframe_height(h);
}
/*]]>*/
</script>
</head>
<body onload="onload_iframe_height">
<div id="childDiv"></div>
</body>
</html>

 

Setting the child file's h at 400 still doesn't make it work.

Link to comment
https://forums.phpfreaks.com/topic/252306-iframe-height/#findComment-1296117
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.