Reputation: 253
I have iframe with content like below,
<iframe frameborder="no" src="http:localhost/com" id="iframe">
<div style="height:850px;width:700px;">div text </div>
</iframe>
This shows the iframe with hori. and vertical scroll but not the height on inner content of 850px.
how can i change the iframe height dynamiclly based on inner content height n width. Im using jquery inside the content.
Its cross domain js widget so most of the below answers throwing permission denied error
Thanxs, Nithish
Upvotes: 1
Views: 20211
Reputation: 61
I found this here: stackoverflow.com/questions/1145850/
and expanded upon it to re-size width also...you just add this in your head:
<script type="text/javascript">
function getDocHeight(doc) {
doc = doc || document;
// stackoverflow.com/questions/1145850/
var body = doc.body, html = doc.documentElement;
var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
return height;
}
function getDocWidth(doc) {
doc = doc || document;
// stackoverflow.com/questions/1145850/
var body = doc.body, html = doc.documentElement;
var width = Math.max( body.scrollWidth, body.offsetWidth,
html.clientWidth, html.scrollWidth, html.offsetWidth );
return width;
}
function setIframeSize(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 ...
ifrm.style.width = "10px"; // reset to minimal width ...
// IE opt. for bing/msn needs a bit added or scrollbar appears
ifrm.style.height = getDocHeight( doc ) + 4 + "px";
ifrm.style.width = getDocWidth( doc ) + 4 + "px";
ifrm.style.visibility = 'visible';
}
</script>
Then make sure you have a unique id in your iframe and call the SetIframeSize:
<iframe src="source.html" id="uniqueid" onload="setIframeSize(this.id)"></iframe>
Upvotes: 0
Reputation: 21
var newHeight = $("iframe[id='iframeAutenticador']").contents().find("html").height();
$("iframe[id='iframeAutenticador']").height(newHeight);
use this.
Upvotes: 2
Reputation: 22485
Nithish,
if you are using jquery, then you can easily find (and set) the height of any div (in your domain - not on external content) as such:
// get the height - in case we want to factor it into the equation below!!
var myDivHeight = $('iframe').css('height');
// set the height
var docHeight = $(document).css('height');
$('iframe').css('height', docHeight);
hope this helps ...
Upvotes: 2
Reputation: 541
You should create a function to measure the height of the content, and set the IFrame height and then call the resize function when the content is loaded.
<SCRIPT LANGUAGE="JavaScript">
function resizeIframeToFitContent(iframe) {
// This function resizes an IFrame object
// to fit its content.
// The IFrame tag must have a unique ID attribute.
iframe.height = document.frames[iframe.id]
.document.body.scrollHeight;
}
</SCRIPT>
Upvotes: 1
Reputation: 4617
If domains are the same for both pages:
parent.document.getElementById("iframe").style.height = $(document).height()+"px";
You can use jQuery in parent page too (if its available there).
Upvotes: 0