Hi Keebs,
Your code produces the correct alerts on my machine (using FF), but doesn't seem to correctly resize the iframes. If you just want a simple script that works (no animation), you can do this:
main.html:
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Setting Iframe Element Height to Iframe Content Height</title>
<script>
/* free code from dyn-web.com */
function getDocHeight(doc) {
doc = doc || document;
// from http://stackoverflow.com/questions/1145850/get-height-of-entire-document-with-javascript
var body = doc.body, html = doc.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
return height;
}
function setIframeHeight(id) {
var ifrm = document.getElementById(id);
var doc = ifrm.contentDocument? ifrm.contentDocument: ifrm.contentWindow.document;
ifrm.style.visibility = 'hidden';
ifrm.style.height = "10px"; // reset to minimal height in case going from longer to shorter doc
ifrm.style.height = getDocHeight( doc ) + 5+"px";
ifrm.style.visibility = 'visible';
}
</script>
</head>
<body ><div style="position: relative; margin: auto; width:100%">
<h1 style="margin-top: 10px; text-align: center">Setting Iframe Element Height to Iframe Content Height</h1>
<div style="position: relative; text-align: center; margin-bottom:10px">
<a href="javascript: void(0)" onclick="frames.some_name.location.replace('page1.html'); return false">page 1</a>
<a href="javascript: void(0)" onclick="frames.some_name.location.replace('page2.html'); return false">page 2</a>
<a href="javascript: void(0)" onclick="frames.some_name.location.replace('page3.html'); return false">page 3</a>
</div>
<div style="position: relative; margin: auto; width:400px; background:white; border: 1px solid black">
<iframe id="some_id" name="some_name" src="page1.html" style="width:100%; text-align: center" frameborder="0" scrolling="no" onload="setIframeHeight(this.id)"></iframe>
</div>
</div></body>
</html>
page1.html (first iframe):
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body style="font-family: verdana; font-size:12px; "><div>
<h2 style="text-align: center">PAGE 1<br>The height of this iframe is auto-adjusted to its content's height</h2>
<div style="text-align: center">
<a href="javascript: void(0)" onclick="parent.frames.some_name.location.replace('page1.html'); return false">page 1</a>
<a href="javascript: void(0)" onclick="parent.frames.some_name.location.replace('page2.html'); return false">page 2</a>
<a href="javascript: void(0)" onclick="parent.frames.some_name.location.replace('page3.html'); return false">page 3</a>
</div>
<br>The dog (Canis lupus familiaris), is a domesticated subspecies of the Gray Wolf, a member of the Canidae family of the order Carnivora. <br><br>The term is used for both feral and pet varieties. <br><br>The domestic dog has been one of the most widely kept working and companion animals in human history.
</div></body>
</html>
page2.html (second iframe):
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body style="font-family: verdana; font-size:12px; "><div>
<h2 style="text-align: center">PAGE 2<br>The height of this iframe is auto-adjusted to its content's height</h2>
<div style="text-align: center">
<a href="javascript: void(0)" onclick="parent.frames.some_name.location.replace('page1.html'); return false">page 1</a>
<a href="javascript: void(0)" onclick="parent.frames.some_name.location.replace('page2.html'); return false">page 2</a>
<a href="javascript: void(0)" onclick="parent.frames.some_name.location.replace('page3.html'); return false">page 3</a>
</div>
<br>The dog (Canis lupus familiaris), is a domesticated subspecies of the Gray Wolf, a member of the Canidae family of the order Carnivora. <br><br>The term is used for both feral and pet varieties. <br><br>The domestic dog has been one of the most widely kept working and companion animals in human history. <BR><BR>
<br>The dog (Canis lupus familiaris), is a domesticated subspecies of the Gray Wolf, a member of the Canidae family of the order Carnivora. <br><br>The term is used for both feral and pet varieties. <br><br>The domestic dog has been one of the most widely kept working and companion animals in human history. <BR><BR>
<br>The dog (Canis lupus familiaris), is a domesticated subspecies of the Gray Wolf, a member of the Canidae family of the order Carnivora. <br><br>The term is used for both feral and pet varieties. <br><br>The domestic dog has been one of the most widely kept working and companion animals in human history.
</div></body>
</html>
page3.html (third iframe)
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body style="font-family: verdana; font-size:12px; "><div>
<h2 style="text-align: center">PAGE 3<br>The height of this iframe is auto-adjusted to its content's height</h2>
<div style="text-align: center">
<a href="javascript: void(0)" onclick="parent.frames.some_name.location.replace('page1.html'); return false">page 1</a>
<a href="javascript: void(0)" onclick="parent.frames.some_name.location.replace('page2.html'); return false">page 2</a>
<a href="javascript: void(0)" onclick="parent.frames.some_name.location.replace('page3.html'); return false">page 3</a>
</div>
<br>The dog (Canis lupus familiaris), is a domesticated subspecies of the Gray Wolf, a member of the Canidae family of the order Carnivora. <br><br>The term is used for both feral and pet varieties. <br><br>The domestic dog has been one of the most widely kept working and companion animals in human history.<BR><BR>
<br>The dog (Canis lupus familiaris), is a domesticated subspecies of the Gray Wolf, a member of the Canidae family of the order Carnivora. <br><br>The term is used for both feral and pet varieties. <br><br>The domestic dog has been one of the most widely kept working and companion animals in human history.<BR><BR>
<br>The dog (Canis lupus familiaris), is a domesticated subspecies of the Gray Wolf, a member of the Canidae family of the order Carnivora. <br><br>The term is used for both feral and pet varieties. <br><br>The domestic dog has been one of the most widely kept working and companion animals in human history.<BR><BR>
<br>The dog (Canis lupus familiaris), is a domesticated subspecies of the Gray Wolf, a member of the Canidae family of the order Carnivora. <br><br>The term is used for both feral and pet varieties. <br><br>The domestic dog has been one of the most widely kept working and companion animals in human history.<BR><BR>
<br>The dog (Canis lupus familiaris), is a domesticated subspecies of the Gray Wolf, a member of the Canidae family of the order Carnivora. <br><br>The term is used for both feral and pet varieties. <br><br>The domestic dog has been one of the most widely kept working and companion animals in human history.<BR><BR>
<br>The dog (Canis lupus familiaris), is a domesticated subspecies of the Gray Wolf, a member of the Canidae family of the order Carnivora. <br><br>The term is used for both feral and pet varieties. <br><br>The domestic dog has been one of the most widely kept working and companion animals in human history.
</div></body>
</html>
Tested and working with all modern browsers. (IE and Chrome only handle this well online).
Bookmarks